2014-03-01 83 views
9

如何在用戶會話打開時獲取用戶名。我嘗試了Facebook sdk示例中的會話登錄示例。如果有人知道解決方案,請幫助我。 在此先感謝。如何從iOS中的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]; 
      }]; 
     } 
    } 
+0

https://developers.facebook.com/docs/ios/graph –

回答

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

更新:id使用objectID屬性,而不是,(2014年5月12日)版本v3.14.1的釋放後,id屬性已被棄用

NSString *facebookId = user.objectID; 
+0

它不起作用 –

+0

@PermerMohamedThabib你有什麼錯誤?你有回電話嗎? – Mani

+0

是的,但fbfetchRequest沒有讀取 –

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

感謝

+0

我需要使用這段代碼嗎? –

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

- (空)loginViewFetchedUserInfo:(FBLoginView *)loginView用戶:(ID)用戶{

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

}

您可以實現此委託方法。

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 

       } 

      } 
     }) 
相關問題