Using this methods Facebook iOS 6 - get user info I can get the profile user info from Facebook, but in the json file are not retrieving the email of the user.
Can you helpme to get the email??
Thanks in advance!
You should get email in this way:
[user objectForKey:#"email"]//Where user is FBGraphUser type
Here is the implementation when login is successful
case FBSessionStateOpen: {
NSLog(#"accessToken: %#", session.accessTokenData.accessToken);
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (error) {
//error
}else{
NSLog(#"%#, %#",[user objectForKey:#"email"], user.description);
}
}];
}
break;
Try this :
facebook = [[Facebook alloc] initWithAppId:[self _APP_KEY] andDelegate:self];
[facebook authorize:[NSArray arrayWithObjects:#"email",nil]];
Related
Hi I am trying to post on facebook from my iOS app, before it worked fine. Now I use the latest SDK and following code.
-(void)Authentication{
if (FBSession.activeSession.isOpen) {
[self promptUserWithAccountNameForUploadPhoto];
} else {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_actions",
nil]; // Tried publish_stream too
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// if login fails for any reason, we alert
if (error) {
// show error to user.
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
[self promptUserWithAccountNameForUploadPhoto];
}
}];
}
}
But when it launches Facebook app and login is done, it doen't notify the user about "Post on your behalf", it notifies my app requires your profile details instead.
And when I try to Post like below
-(void)promptUserWithAccountNameForUploadPhoto {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
NSString * msgStr = [NSString stringWithFormat:#"I have added a new Trip to %#",place];
NSString *imgStr = [NSString stringWithFormat:#"%#assets/upload/flags/%#-213x142.png",appDelegate.ServerURL,country];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Sharing Tutorial", #"name",
#"Build great social apps and get more installs.", #"caption",
#"Allow your users to share stories on Facebook from your app using the iOS SDK.", #"description",
#"https://developers.facebook.com/docs/ios/share/", #"link",
#"http://i.imgur.com/g3Qc1HN.png", #"picture",
[FBRequestConnection startWithGraphPath:#"/me/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Link posted successfully to Facebook
NSLog(#"result: %#", result);
} else {
NSLog(#"%#", error.description);
}
}];
}
}];
}
An error comes as -
message = "(#200) The user hasn't authorized the application to perform this action";
from error code it looks like you don't have permission for share
see facebook error codehere
for get sharing permission refer this page
This error is thrown when you are using publish_actions permission without review. For testing purpose you can always make a test user in Roles column of MyApp in developers.facebook.com and then use it..
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 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);
}];
}
In the "old" FB iOS SDK I could receive user information via the following:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"SELECT uid, name, email, pic FROM user WHERE uid=me()", #"query",
nil];
JBFacebookManager *fbManager = [JBFacebookManager getInstance];
fbManager.requestDelegate = self;
[fbManager.facebook requestWithMethodName:#"fql.query"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
How can I do this with the new FB iOS SDK 3.0? Do I need to use FBRequest or FBOpenGraphAction or FBGraphObject or a combination of those or something completely different?
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
self.nameLabel.text = user.name;
self.emailLabel.text = [user objectForKey:#"email"];
}
}];
}
Because FBGraphUser doesn't have an email #property, we can't access the information like with the name (dot-syntax), however the NSDictionary still has the email kv-pair and we can access it like we would do with a normal NSDictionary.
Don't forget to ask for the email permission though:
NSArray *permissions = [[NSArray alloc] initWithObjects:#"email", nil];
[FBSession sessionOpenWithPermissions:permissions completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
[self facebookSessionStateChanged:session state:state error:error];
}];
Once you have access to (id<FBGraphUser>)user, you could simply use, user[#"email"].
from this we can get facebook user's basic info and email id.
[FBSession openActiveSessionWithReadPermissions:#[#"basic_info",#"email"] allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState status,NSError *error){
if(error)
{
NSLog(#"Facebook Error : %#",error.localizedDescription);
}
else{
}
// Respond to session state changes,
// ex: updating the view
}];
Did you read the source code of the 3.0 SDK? There is a method that I think is identical:
- (FBRequest*)requestWithMethodName:(NSString *)methodName
andParams:(NSMutableDictionary *)params
andHttpMethod:(NSString *)httpMethod
andDelegate:(id <FBRequestDelegate>)delegate;
The easiest method for getting the info after logging in is :
-(IBAction)loginWithFacebook:(id)sender{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: #[#"public_profile",#"email"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(#"Process error");
} else if (result.isCancelled) {
NSLog(#"Cancelled");
} else {
NSLog(#"Logged in");
[self getFacebookProfileInfos];
}
}];
}
-(void)getFacebookProfileInfos {
NSDictionary *parameters = # {#"fields": #"id, name, first_name, last_name, picture.type(large), email"};
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:parameters]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"fetched user:%#", result);
}
}];
}
}
You will get all the info the result.