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"];
}];
Related
I am trying fetch my Friends who are using my application from facebook and but in the response i am getting empty list list. Please guide me what i am doing wrong. I am using the following code
[[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (error) {
//error
NSLog(#"%#",error);
}else{
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary* result,NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"%#",result);
for (NSDictionary<FBGraphUser>* friend in friends)
{
NSLog(#"%# %d",friend.username,[friend.id intValue]);
}
}];
}
}];
Thanks
Possible reasons:
1) You don't have the rights to get connections of this account (wrong apikey/passkey or rights not properly set)
2) Check what is stored in "result" variable and the answer status: it may guide you in some way.
3) Finally, try to get some simpler informations (like your own username) to be sure you have access to these informations.
In the new version of Facebook's API you will get ONLY friends that installed the app (i.e. is_app_user is 1).
You can't get friends who haven't logged in with your app anymore.
Instead, you can get taggable_friends. This will return a list with all of your friends, but here "id" field will be different, not the real facebook id. Its only for tagging, i.e. for passing to field "tags" like:
NSDictionary *params = #{#"message":self.invitationText.text,
#"tags":ids,
#"caption":#"...",
#"name":#"...",
#"place":#"123456789",
#"link":APP_STORE_URL};
[FBRequestConnection startWithGraphPath:#"/me/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
//...
}];
I am using Facebook SDK in my iPhone app. I want to find the user's Facebook profile photos, for that I am using the below code:
-(IBAction)FacebookLogin:(id)sender{
if (FBSession.activeSession.isOpen) {
[self findAlbums];
} else {
NSArray *permissions = [[NSArray alloc] initWithObjects:#"user_photos",
nil];
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// if login fails for any reason, we alert
if (error) {
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
[self findAlbums];
}
}];
}
}];
}
}
-(void)findAlbums {
[FBRequestConnection startWithGraphPath:#"/me/albums"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
NSLog(#"result::%#",result);
}];
}
Out put ---- data = ( );
This does not give any albums in data. The Facebook user logged in have many albums and photos in his profile. Why this happens?
just change the code in find albums to :
[FBRequestConnection startWithGraphPath:#"me/albums"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Success! Include your code to handle the results here
NSLog(#"user events: %#", result);
NSArray *feed =[result objectForKey:#"data"];
for (NSDictionary *dict in feed) {
NSLog(#"first %#",dict);
}
} else {
// An error occurred, we need to handle the error
// Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
NSLog(#"error %#", error.description);
}
}];
you have to add the Facebook credentials(i.e facebook account) you're using to the Facebook developer account settings(i.e as admin or developer) , only then you can access the album's photos through your code
I am using facebook SDK for iOS in my app.
-(IBAction)FacebookLogin:(id)sender{
if (FBSession.activeSession.isOpen) {
[self promptUserWithAccountNameForStatusUpdate];
}
else {
NSArray *permissions = [[NSArray alloc] initWithObjects: #"publish_stream",#"email",nil];
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
if (error) {
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
[self promptUserWithAccountNameForStatusUpdate];
}
}];
}
}];
}
}
-(void)promptUserWithAccountNameForStatusUpdate {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
}
}];
}
How can I fetch profile photos of user ?
I want to fetch his previous 4 profile photos also.
Just like in the app Tinder.
How to do this ?
Ok..I found that
We will get all albums with the below url
https://graph.facebook.com/[uid]/albums?access_token=[AUTH_TOKEN]
Then we can pass album ID to below url
https://graph.facebook.com/[ALBUM_ID]/photos?access_token=[AUTH_TOKEN]
We will get all profile images...bingo :) :) ;)
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?
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);
}];
}