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

∥☆過路亽.° 提交于 2019-12-05 19:30:59

You can't fetch users birthdate only yours you can get and you have to send app in review for approve in facebook developer after than if your app approve than you can set extra permissions but i don't think that users birthday you can get.

Avinash651

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!