I am making an app login feature with Facebook account. I have the Facebook tokenkey by running this command: appDelegate.session.accessTokenData.accessToken. Then I take username and user_ID like this:
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error == nil) {
// result is a dictionary with the user's Facebook data
NSDictionary *userData = (NSDictionary *)result;
NSString *facebookID = userData[#"id"];
NSLog(#"facebookID = %#",facebookID);
or
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
self.userNameLabel.text = user.name;
self.userProfileImage.profileID = user.id;
}
}];
}
the result is the same and when I debug It always jump up in if(!error).
Where does this error come from?
Related
is it possible, using the Facebook SDK, to get the user info from his user id? An important note is that the user would have approved the application and its permissions before the app make the request.
If it is possible, what is the right way to achieve this? I am trying using this code:
[FBRequestConnection startWithGraphPath:#"/100007046299250"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
}];
But I get the following error message:
Error for request to endpoint '/100007046299250': An open FBSession must be specified for calls to this endpoint.
Thanks
After login authentication, you can get details from active FBSession like below
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
NSString *firstName = user.first_name;
NSString *lastName = user.last_name;
NSString *facebookId = user.id;
NSString *email = [user objectForKey:#"email"];
NSString *imageUrl = [[NSString alloc] initWithFormat: #"http://graph.facebook.com/%#/picture?type=large", facebookId];
}
}];
}
view.m
#interface ViewController ()
{
NSMutableArray *fbData,*userInfoArray;
NSString *name;
NSString *picture;
NSString *source;
NSMutableArray *myObject;
NSDictionary *dictionary,*userData;
}
code is describeDictionary function
void describeDictionary (NSDictionary *dict)
{
NSArray *keys;
int i, count;
id key, value;
keys = [dict allKeys];
count = [keys count];
for (i = 0; i < count; i++)
{
key = [keys objectAtIndex: i];
value = [dict objectForKey: key];
NSLog (#"Key: %# for value: %#", key, value);
}
}
code is Data
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"]; // The result is a dictionary
describeDictionary(userData);
}
}];
code is not Data
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"]; // The result is a dictionary
}
}];
describeDictionary(userData);
Data should be identical
help me please i am get Facebook graph api
To retrieve the data used But not available
I do not know what to do then. Find out how to do it.
In your last code block:
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"]; // The result is a dictionary
}
}];
describeDictionary(userData);
The describeDictionary(userData); line is in the wrong place and that's why it's nil—you haven't completed the graph object request at the point you call describeDictionary. It should be like this:
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"]; // The result is a dictionary
describeDictionary(userData);
}
}];
To explain further, look at your original code:
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"]; // The result is a dictionary
}
}];
describeDictionary(userData);
This is the sequence in which the code is executed:
First, you create the FBRequest object that will be sent to the Facebook API:
FBRequest *request = [FBRequest requestForGraphPath:#"xxxx?fields=videos.fields(name,picture,source)"];
Next, you tell the request object to call the Facebook API and execute the completion handler when it has finished:
[request startWithCompletionHandler:... ];
Next you call describeDictionary, but userData is neither in scope or initialized. The request's completion handler has not yet been executed therefore userData is nil at this point.
describeDictionary(userData);
The completion handler is called asynchronously when the request has finished. It may have finished successfully—in which case user will not be nil—or there may be an error condition. If there is no error, you assign the value of the videos key of the user object to userData. It is only at this point that can you pass userData to describeDictionary:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
userData = user[#"videos"];
}
}
You have to call describeDictionary from within the completion handle block.
I am trying to fetch work info for FB friends on iOS using Facebook SDK. Here's the code that I am using:
if (FBSession.activeSession.isOpen) {
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"work", #"fields",
nil];
FBRequest *request = [FBRequest requestWithGraphPath:#"me/friends" parameters:params HTTPMethod:#"GET"];
[request startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary *result, NSError *error) {
if (!error) {
if (result) {
if (result.count) {
NSMutableArray *friendsWithJobTitles = [NSMutableArray array];
for (id aFriend in [result objectForKey:#"data"]) {
if ([aFriend objectForKey:#"work"]) {
[friendsWithJobTitles addObject:aFriend];
}
}
NSLog(#"result = %#", friendsWithJobTitles);
}
}
}
}];
}
The friendsWithJobTitles is always blank for some reason. Please help.
a question about Facebook sdk for iOS.
If I have an active session how can I get only the user ID?
Is there a simple way to obtain this id without use this example code?
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *aUser, NSError *error) {
if (!error) {
// I can take the user id..
}
}];
}
+ (NSString *)activeUserId {
return [FBSDKAccessToken currentAccessToken].userID;
}
if you are already logged in on facebook.
This is best way to take user id, instead of directly access using dot.
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *aUser, NSError *error) {
if (!error) {
NSLog(#"User id %#",[aUser objectForKey:#"id"]);
}
}];
[FBRequestConnection startWithGraphPath:#"/me"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
NSDictionary *result,
NSError *error
) {
/* handle the result */
_fbId = [result objectForKey:#"id"];
}];
I am running into issues trying upgrade my Facebook SDK to the latest production release (FacebookSDK-3.0.8.pkg - Facebook SDK 3.0 for iOS (update 1) [August 21, 2012]).
I am following along with tutorial on this page.
I have ran into several issues trying to get the code to work, it's not as easy as it proclaims to be in the tutorial. I can get my session open, but can not get the request to work.
- (IBAction)facebookTapped:(id)sender {
[FBSession openActiveSessionWithPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if(error) {
NSLog(#"Error opening session: %#", error);
return;
}
if(session.isOpen) {
NSLog(#"session is open");
FBRequest *me = [FBRequest requestForGraphPath:#"me"];
[me startWithCompletionHandler:^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *my,
NSError *error) {
NSLog(#"My name: %#", my.first_name);
}];
}
}];
}
My console displays that the session is open if I remove the call to FBRequest requestforGraphpath. If I leave it in, I receive the error "Incompatible block pointer types initializing 'void(^)(struct FBRequestConection , struct NSDictionary, struct NSError*)', expected 'FBRequestHandler'
Now what has me stumped is that this is the exact code shown in the tutorial, excpet that I changed out [FBRequest requestForMe] trying different approaches. None worked.
Can anyone shed some light on this for me?
Thank you.
I was able to solve this issue by changing their original block in the tutorial of:
if (session.isOpen) {
FBRequest *me = [FBRequest requestForMe];
[me startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *my,
NSError *error) {
self.label.text = my.first_name;
}];
}
to
if(session.isOpen) {
FBRequest *me = [FBRequest requestForMe];
[me startWithCompletionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSDictionary<FBGraphUser> *my = (NSDictionary<FBGraphUser> *) result;
NSLog(#"My dictionary: %#", my.first_name);
}];
}