9

如何在用户会话打开时获取用户名。我尝试了 Facebook sdk 示例中的会话登录示例。如果有人知道解决方案,请帮助我。提前致谢。

 - (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];
            }];
        }
    }
4

5 回答 5

32

登录身份验证后,您可以从活动的 FBSession 中获取详细信息,如下所示

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];
         }
     }];
}

更新:在版本 v3.14.1(2014 年 5 月 12 日)发布后,该属性已被弃用,而不是id使用属性objectIDid

NSString *facebookId = user.objectID;
于 2014-03-01T09:28:10.990 回答
6

您好,您可以获取 Facebook 用户的详细信息,例如:

- (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];
    }
}

谢谢

于 2014-03-01T09:02:09.653 回答
1
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;
}
于 2014-03-01T09:12:16.537 回答
1
        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

                }

            }
        })
于 2016-10-05T12:09:12.777 回答
-1
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user {
  
    
    NSLog(@"user facebook details:%@",user.objectID);
    
   }

您可以实现此委托方法。

于 2014-12-16T10:29:28.177 回答