How to get user info from Facebook SDK in iOS?

為{幸葍}努か 提交于 2019-11-28 03:17:34

问题


How to get user name when user session is open.I tried the session login sample from Facebook sdk samples. if any one knows the solution please help me out. Thanks in advance.

 - (IBAction)buttonClickHandler:(id)sender {
        // get the app delegate so that we can access the session property
        SLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

        // this button's job is to flip-flop the session from open to closed
        if (appDelegate.session.isOpen) {
            // if a user logs out explicitly, we delete any cached token information, and next
            // time they run the applicaiton they will be presented with log in UX again; most
            // users will simply close the app or switch away, without logging out; this will
            // cause the implicit cached-token login to occur on next launch of the application
   //hear i need to fetch user name ?


            [appDelegate.session closeAndClearTokenInformation];

        } else {
            if (appDelegate.session.state != FBSessionStateCreated) {
                // Create a new, logged out session.
                appDelegate.session = [[FBSession alloc] init];
            }

            // if the session isn't open, let's open it now and present the login UX to the user
            [appDelegate.session openWithCompletionHandler:^(FBSession *session,
                                                             FBSessionState status,
                                                             NSError *error) {
                // and here we make sure to update our UX according to the new session state
                [self updateView];
            }];
        }
    }

回答1:


After login authentication, you can get details from active FBSession like below

if (FBSession.activeSession.isOpen) {

    [[FBRequest requestForMe] startWithCompletionHandler:
     ^(FBRequestConnection *connection,
       NSDictionary<FBGraphUser> *user,
       NSError *error) {
         if (!error) {
             NSString *firstName = user.first_name;
             NSString *lastName = user.last_name;
             NSString *facebookId = user.id;
             NSString *email = [user objectForKey:@"email"];
             NSString *imageUrl = [[NSString alloc] initWithFormat: @"http://graph.facebook.com/%@/picture?type=large", facebookId];
         }
     }];
}

Update: Instead of id use objectID property, after release of version v3.14.1(May 12, 2014), the id property has been deprecated

NSString *facebookId = user.objectID;



回答2:


Hi you can get the details of Facebook user like:

- (void)sessionStateChanged:(NSNotification*)notification {
    if ([[FBSession activeSession] isOpen]) {
        [FBRequestConnection
         startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                           id<FBGraphUser> user,
                                           NSError *error) {
             if (! error) {

                //details of user
                NSLog(@"User details =%@",user);
             }
         }];
    }else {
        [[FBSession activeSession] closeAndClearTokenInformation];
    }
}

Thanks




回答3:


if (!error && state == FBSessionStateOpen){
    NSLog(@"Session opened");
    // Show the user the logged-in UI
     FBRequest* userupdate = [FBRequest requestWithGraphPath:@"me" parameters: [NSMutableDictionary dictionaryWithObject:@"picture.type(large),id,birthday,email,gender,username,name,first_name" forKey:@"fields"] HTTPMethod:@"GET"];
    [drk showWithMessage:nil];
    [userupdate startWithCompletionHandler: ^(FBRequestConnection *connection,
                                          NSDictionary* result,NSError *error)
   {
    NSLog(@"dict = %@",result);
   }];
    return;
}



回答4:


        FBSDKGraphRequest(graphPath: "me", parameters: nil).startWithCompletionHandler({ (connection, object, error) in
            guard let user = object as? Dictionary<String, NSObject> else {
                alert("facebook sdk gave me trash object \(object)")
                return
            }
            if let error = error {
                alert(error.localizedDescription)
            }
            else {
                if let userID = user["id"] {
                   // gr8t success

                }

            }
        })



回答5:


-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id)user {

NSLog(@"user facebook details:%@",user.objectID);

}

You can implement this delegate method.



来源:https://stackoverflow.com/questions/22111993/how-to-get-user-info-from-facebook-sdk-in-ios

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