Safari
Safari

Reputation: 11935

Facebook iOS sdk to take user id

a question about Facebook sdk for iOS.

If I have an active session how can I get only the user ID?

Is there a simple way to obtain this id without use this example code?

if (FBSession.activeSession.isOpen) {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *aUser, NSError *error) {
             if (!error) {
                 // I can take the user id..
             }
         }];
    }

Upvotes: 11

Views: 8288

Answers (3)

bonpv
bonpv

Reputation: 91

+ (NSString *)activeUserId {
    return [FBSDKAccessToken currentAccessToken].userID;
}

if you are already logged in on facebook.

Upvotes: 9

Cayke Prudente
Cayke Prudente

Reputation: 219

[FBRequestConnection startWithGraphPath:@"/me"
                             parameters:nil
                             HTTPMethod:@"GET"
                      completionHandler:^(
                                          FBRequestConnection *connection,
                                          NSDictionary *result,
                                          NSError *error
                                          ) {
                          /* handle the result */
                          _fbId = [result objectForKey:@"id"];
                      }];

Upvotes: 1

Mani
Mani

Reputation: 17585

This is best way to take user id, instead of directly access using dot.

[[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *aUser, NSError *error) {
             if (!error) {
                 NSLog(@"User id %@",[aUser objectForKey:@"id"]);
             }
         }];

Upvotes: 8

Related Questions