I am currently working on Facebook SDK 4.4.0. Before 4.4.0 version, in 4.3.0 we were getting email, public_profile with the following code
NSArray *arrFBPermission = [[NSArray alloc]initWithObjects:#"email",#"public_profile", nil];
[loginUsingFB logInWithReadPermissions:arrFBPermission handler:^(FBSDKLoginManagerLoginResult *result, NSError *error){
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error){
if (!error){
NSString *fnm = [result valueForKey:#"first_name"];
NSString *lnm = [result valueForKey:#"last_name"];
}
else{
Nslog("%#", error localizedDescription);
}
}];
}
Now in the New SDK (i.e. 4.4.0) I am not getting these values. Why? I want email, first_name, last_name, id from Facebook.
With Facebook 4.4 SDK there is a slight change.
You must have to request the parameter with the FBSDKGraphRequest which you want from Facebook account.
In your code there is nil in parameters :
Update your code with the parameters like as :
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"}]
If you want to login with Facebook with the use of Custom Button then you can use the complete code as follows :
- (IBAction)btnFacebookPressed:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior = FBSDKLoginBehaviorBrowser;
[login logInWithReadPermissions:#[#"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
}
else if (result.isCancelled)
{
// Handle cancellations
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
NSLog(#"result is:%#",result);
[self fetchUserInfo];
[login logOut]; // Only If you don't want to save the session for current app
}
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(#"Token is available : %#",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"resultis:%#",result);
}
else
{
NSLog(#"Error %#",error);
}
}];
}
}
I hope it will work for you.
Related
I'm trying to make registration in my iOS app through facebook SDK
I am using FBSDKLoginManager for Login in to facebook from that I am getting AccessToken which I have verified.
https://graph.facebook.com/me?access_token=tokenstring
I got the name and Id in response but I need to get email also for registration on Qiuckblox.
I have downloaded q-municate app(based quickblox.com) from github and checked token.
I have same tokens but result is different. How come?
Maybe I should set some settings in facebook?
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut];
[login logInWithReadPermissions:#[#"email", #"public_profile", #"user_friends"]
fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
NSLog(#"%#",error.localizedDescription);
}
else if (result.isCancelled) {
// Handle cancellations
}
else {
NSLog(#"%#",result.token.tokenString);
if ([result.grantedPermissions containsObject:#"email"]) {
NSLog(#"Granted all permission");
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"%#",result);
}
}];
}
} else {
NSLog(#"Not granted");
}
}
}];
here screenshots:
http://screencast.com/t/9KNK9HIb
http://screencast.com/t/BDsfYVZi
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, first_name, last_name, picture.type(large), email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"results:%#",result);
NSString *email = [result objectForKey:#"email"];
}
Change like this
You need to explicitly enumerate all the fields you want:
https://graph.facebook.com/v2.5/me?fields=id,name,email,gender&access_token=<access_token>
I have used Facebook SDK from Facebook developer portal and completed all the steps which were written but during login through Facebook from my app by default it uses Safari for login procedure.
Why doesn't it use installed app for this purpose?
Try this.
-(IBAction)CLK_ConnectWithFB:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
// If the Facebook app is not installed on the device, falls back to \c FBSDKLoginBehaviorBrowser. This is the default behavior.
login.loginBehavior = FBSDKLoginBehaviorNative;
[login logInWithReadPermissions:#[#"email", #"user_friends",#"user_posts"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
NSLog(#"error is :%#",error);
}
else if (result.isCancelled)
{
// Handle cancellations
NSLog(#"error is :%#",error);
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
[self fetchUserInfo];
[login logOut];
}
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id,name,link,first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown ,posts, friendlists"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"result is :%#",result);
NSLog(#"friend is :%#",[[result valueForKey:#"friends"]objectForKey:#"data"]);
NSString *photostring=[[[result valueForKey:#"picture"] objectForKey:#"data"] valueForKey:#"url"];
photostring = [photostring stringByReplacingOccurrencesOfString:#"&" withString:#"%26"];
// [self Userlogin:[result valueForKey:#"email"] Username:[result valueForKey:#"name"] PhotoUrl:photostring LoginType:#"fb" Phoneno:#""];
}
}];
}
}
In my app i have to implement facebook login .based on the facebook documentation i am doing integration but when i am fetching users info email section is coming null for me .
can some one tell me where is the issue .
this code i am using
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
//login.loginBehavior = FBSDKLoginBehaviorBrowser;
[login logInWithReadPermissions:#[#"public_profile",#"email",#"user_friends"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
}
else if (result.isCancelled)
{
// Handle cancellations
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
NSLog(#"result is:%#",result);
[login logOut]; // Only If you don't want to save the session for current app
}
}
}];
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(#"Token is available : %#",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"fetched user:%# and Email : %#", result,result[#"email"]);
}
}];
}
}
getting out put this
fetched user:{
id = 970029739773026393591;
name = "jace";
} and Email : (null)
FBSDKLog: starting with Graph API v2.4, GET requests for /me should contain an explicit "fields" parameter
The Facebook has upgraded the permissions, so follow like this
change this line
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil]
into
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"}]
additional reference
You will have to pass paramenter email to fetch user_email through Facebook:
///Add This line
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
[parameters setValue:#"id,name,email" forKey:#"fields"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:parameters] <--------- This line
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
}
you have to get user email like this
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"email", #"user_friends",#"user_birthday"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
NSLog(#"error is :%#",error);
}
else if (result.isCancelled)
{
// Handle cancellations
NSLog(#"error is :%#",error);
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
NSLog(#"Login successfull");
[self fetchUserInfo];
}
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id,name,link,first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"result : %#",result);
}
}];
}
}
I am trying to log in with facebook using api v2.4 and using custom button for login but I am not getting success. I am able to get facebook id but rest info is coming null. I have done the same as in the earlier version of api it is working well. Can anyone tell me what should I do now?
Your help would be appreciated.
You try this way to get information.
And facebook SDK 4.x get any information then pass parameter request fields like this way
parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}
- (IBAction)btnFacebookPressed:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
}
else if (result.isCancelled)
{
// Handle cancellations
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
NSLog(#"result is:%#",result);
[self fetchUserInfo];
}
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(#"Token is available : %#",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"resultis:%#",result);
}
else
{
NSLog(#"Error %#",error);
}
}];
}
}
I am currently working on Facebook SDK 4.4.0. Before 4.4.0 version, in 4.3.0 we were getting email, public_profile with the following code
NSArray *arrFBPermission = [[NSArray alloc]initWithObjects:#"email",#"public_profile", nil];
[loginUsingFB logInWithReadPermissions:arrFBPermission handler:^(FBSDKLoginManagerLoginResult *result, NSError *error){
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error){
if (!error){
NSString *fnm = [result valueForKey:#"first_name"];
NSString *lnm = [result valueForKey:#"last_name"];
}
else{
Nslog("%#", error localizedDescription);
}
}];
}
Now in the New SDK (i.e. 4.4.0) I am not getting these values. Why? I want email, first_name, last_name, id from Facebook.
With Facebook 4.4 SDK there is a slight change.
You must have to request the parameter with the FBSDKGraphRequest which you want from Facebook account.
In your code there is nil in parameters :
Update your code with the parameters like as :
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location , friends ,hometown , friendlists"}]
If you want to login with Facebook with the use of Custom Button then you can use the complete code as follows :
- (IBAction)btnFacebookPressed:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior = FBSDKLoginBehaviorBrowser;
[login logInWithReadPermissions:#[#"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
}
else if (result.isCancelled)
{
// Handle cancellations
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
NSLog(#"result is:%#",result);
[self fetchUserInfo];
[login logOut]; // Only If you don't want to save the session for current app
}
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(#"Token is available : %#",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id, name, link, first_name, last_name, picture.type(large), email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"resultis:%#",result);
}
else
{
NSLog(#"Error %#",error);
}
}];
}
}
I hope it will work for you.