简体   繁体   中英

How to fetch user's birthday from Facebook API in iOS using latest Facebook sdk?

How to fetch user's birthday from Facebook API in iOS using latest Facebook sdk?

I have tried to fetch it

 {@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday"}

and have given permission

[login logInWithReadPermissions:@[@"public_profile", @"email", @"user_friends",@"user_birthday"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)

But not able the fetch birthday.

您不能仅获取您可以获取的用户生日,并且必须在应用程序批准后将应用程序发送给Facebook开发者以供批准,而不是您的应用程序可以设置额外的权限,但我认为您无法获得用户的生日。

Use this code.This may help you

(IBAction)btnfb:(id)sender

    {
      if (!FBSession.activeSession.isOpen)
            {
                NSArray *_fbPermissions =    @[@"email",@"publish_actions",@"public_profile",@"user_hometown",@"user_birthday",@"user_about_me",@"user_friends",@"user_photos",];

        [FBSession openActiveSessionWithReadPermissions:_fbPermissions allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error)
         {
             if (error)
             {
                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                 [alertView show];
             }
             else if(session.isOpen)
             {
                 [self btn_fb:sender];
             }


         }];


        return;
}
[FBRequestConnection startWithGraphPath:@"me" parameters:[NSDictionary 

dictionaryWithObject:@"cover,picture.type(large),id,name,first_name,last_name,gender,birthday,email,location,hometown,bio,photos" 

forKey:@"fields"] HTTPMethod:@"GET" 

completionHandler:^(FBRequestConnection *connection, id result, NSError 

*error)

     {
         {
             if (!error)
             {
                 if ([result isKindOfClass:[NSDictionary class]])
                 {
                     NSLog(@"%@",result);

                 }
             }
         }
     }];
}

Swift 3x:

Hope this will help anybody

//MARK: - sign in with facebook

let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()

fbLoginManager.logIn(withReadPermissions: ["email","user_photos","user_birthday"], from: self) { (result, error) -> Void in

    if (error == nil) {
            let fbloginresult : FBSDKLoginManagerLoginResult = result!
            print("fbloginresult######:\(fbloginresult)")

            if (result?.isCancelled)! {

                print(result ?? FBSDKLoginManagerLoginResult())

            } else if(fbloginresult.grantedPermissions.contains("email")) {
                //show loader

                //print(user())
                //FETCH USER DATA

                if((FBSDKAccessToken.current()) != nil) {
                    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email, birthday"]).start( completionHandler: { (connection, result, error) -> Void in
                        if (error == nil) {
                            //everything works print the user data
                            print(result ?? AnyObject.self)

                            if let jsondata = result as? [String: Any], let strEmail = jsondata["email"] as? String, let fb_id = jsondata["id"] as? String, let picture = jsondata["picture"] as? [String: Any], let data = picture["data"] as? [String: Any],let url = data["url"] as? String  {

                                // print(url)
                                print(strEmail)
                                print(fb_id)

                            }

                            //Loader hide

                        }

                    })
                }
            }

        }
    }

Try the account in which you have registered your app. Don't try with other Facebook account.

It will give you a Birhtday.

Also check privacy settings of account.

Birthday may not be public. That can also be an issue.

Thanks

Take in account that to get the birthday (and some other attributes like the user bio) the app has to be reviewed by Facebook. Otherwise, although the permissions were given correctly to the user the Graph API call will not retrieve this attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM