适用于iOS的Facebook SDK v4.0 – 未设置FBSDKProfile currentProfile

我刚刚将我的Facebook SDK“升级”为我的iOS应用程序的4.0。

我已经让登录工作正常,但是,根据文档,我现在应该使用FBSDKProfile.currentProfile()来访问配置文件信息。

但是,即使我的个人资料图片正在显示(通过FBSDKProfilePictureView )并且我的FBSDKAccessToken.currentAccessToken()不是nil ,这个值似乎总是nil

这是我的登录ViewController:

 import UIKit class LoginViewController: UIViewController, FBSDKLoginButtonDelegate { @IBOutlet weak var fbLoginButton: FBSDKLoginButton! @IBOutlet weak var fbProfilePicture: FBSDKProfilePictureView! //displays a picture override func viewDidLoad() { super.viewDidLoad() self.fbLoginButton.delegate = self FBSDKProfile.enableUpdatesOnAccessTokenChange(true) if FBSDKAccessToken.currentAccessToken() != nil { println("\(FBSDKAccessToken.currentAccessToken().userID)") //works } self.fbLoginButton.readPermissions = ["public_profile", "email", "user_friends"] } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { println(FBSDKProfile.currentProfile()) //is nil } func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) { } } 

有任何想法吗?

登录回调时可能尚未完成FBSDKProfile提取。 相反,您可以观察“FBSDKProfileDidChangeNotification”通知post。

别忘了FBSDKProfile.enableUpdatesOnAccessTokenChange(true) !! 我挣扎了一段时间,直到我在文档中找到了。 然后订阅FBSDKProfileDidChangeNotification上的通知应该适用于FBSDKLoginButton。

完整示例…我通过Interface Builder连接了loginButton

 class LoginController: UIViewController, FBSDKLoginButtonDelegate { @IBOutlet var loginButton: FBSDKLoginButton! override func viewDidLoad() { super.viewDidLoad() loginButton.delegate = self; FBSDKProfile.enableUpdatesOnAccessTokenChange(true) NSNotificationCenter.defaultCenter().addObserver(self, selector: "onProfileUpdated:", name:FBSDKProfileDidChangeNotification, object: nil) } func onProfileUpdated(notification: NSNotification) { } func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!){ } func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) { } 

}

对于那些正在寻找比使用NSNotificationCenter更新的答案和更简洁的方法的人来说,FBSDKProfile上有一个类方法,您可以在登录后调用它,如下所示:

 [FBSDKProfile loadCurrentProfileWithCompletion:^(FBSDKProfile *profile, NSError *error) { }]; 

我在objc,但也遇到了这个问题。

首先,Johnny Z的回答是正确的,你必须通过将’enableUpdatesOnAccessTokenChange’设置为true然后观察更改来显式启用FBSDKProfile类。 事实上,根据标题评论,这就是FBSDKProfile在内部所做的事情。

但是,正如fanfan在评论中指出的那样,FBSDKProfile并没有你可能感兴趣的所有字段。我最终没有使用FBSDKProfile,只是为’我’做了一个图形请求:

  [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (error) { CLS_LOG(@"Login error: %@", [error localizedDescription]); return; } NSLog(@"fecthed user: %@", result); }]; 

注意:您仍然可以使用“FBSDKProfileDidChangeNotification”通知观察更改。 阅读FBSDKAccessToken.h注释以获取此通知的文档及其将发送的密钥。

在objective-C中,我根据此主题中的注释使用了以下方法。

如果此标志设置为yes,则在登录或注销后将调用通知。 因此,在viewDidLoad中添加此行代码并添加在收到用户配置文件后要调用的通知。

 [FBSDKProfile enableUpdatesOnAccessTokenChange:YES]; [[NSNotificationCenter defaultCenter] addObserverForName:FBSDKProfileDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) { //do whatever you want }]; 

此链接中的所有信息https://developers.facebook.com/docs/facebook-login/ios/v2.3#profiles

在调用enableUpdatesOnAccessTokenChange:YES之前,您似乎需要调用AppDelegate方法来初始化SDK enableUpdatesOnAccessTokenChange:YES

我像这样制作了我的AppDelegate didFinishLaunching方法

BOOL fbsdk = [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; [FBSDKProfile enableUpdatesOnAccessTokenChange:YES]; return fbsdk;

这似乎有效。 如果您需要不在配置文件对象中的项目,您仍可能需要执行/me请求。

我在[FBSDKProfile currentProfile]遇到了类似的问题,但在Objective-C中,您还可以从Graph API获取用户配置文件的属性,并使用[FBSDKProfile currentProfile]响应https://graph.facebook.com/me?access_token=

Swift 4版,

 FBSDKProfile.loadCurrentProfile(completion: { profile, error in let url = profile?.imageURL(for: FBSDKProfilePictureMode.square, size: self.imageview.frame.size) })