代码之家  ›  专栏  ›  技术社区  ›  fanfan

iOS版Facebook SDK v4.0-未设置FBSDKProfile currentProfile

  •  20
  • fanfan  · 技术社区  · 10 年前

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

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

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

    这是我的登录 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!) {
        }
    }
    

    有什么想法吗?

    8 回复  |  直到 10 年前
        1
  •  27
  •   Johnny Z    10 年前

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

    完整示例。。。我通过界面生成器连接登录按钮

    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!) {
    
    }
    

    }

        2
  •  13
  •   Eric Alford    8 年前

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

    [FBSDKProfile loadCurrentProfileWithCompletion:^(FBSDKProfile *profile, NSError *error) {
    
    }];
    
        3
  •  11
  •   Chris Pan    10 年前

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

        4
  •  7
  •   Chad Pavliska    10 年前

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

    首先,Johnny Z的答案是正确的,即必须通过将“enableUpdatesOnAccessTokenChange”设置为true,然后观察更改,显式启用FBSDKProfile类。事实上,根据标题注释,这就是FBSDKProfile在内部所做的。

    然而,正如fanfan在评论中指出的那样,FBSDKProfile并没有你可能感兴趣的所有字段。我最终没有使用FBSDKPprofile,只是像这样对“me”发出了一个图形请求:

        [[[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注释,了解此通知的文档及其将发送的密钥。

        5
  •  6
  •   Pedro Romão    10 年前

    在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

        6
  •  3
  •   Brian Broom    10 年前

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

    我的AppDelegate didFinishLaunching方法如下

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

    这似乎奏效了。你可能还需要做一个 /me 如果您需要配置文件对象中没有的项目,请请求。

        7
  •  2
  •   Aadil Keshwani    9 年前

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

        8
  •  0
  •   cnu    7 年前

    Swift 4版本,

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