Facebook SDK v4.0 for iOS - FBSDKProfile currentProfile not being set

女生的网名这么多〃 提交于 2019-11-30 01:49:25

The FBSDKProfile fetch may not have been completed by the time of the login callback. Instead you can observe for a 'FBSDKProfileDidChangeNotification' notification post.

Don't forget FBSDKProfile.enableUpdatesOnAccessTokenChange(true)!! I struggled with this for awhile until I found that in the docs. Then subscribing to notifications on FBSDKProfileDidChangeNotification should work for the FBSDKLoginButton.

Full Example... I hook up loginButton via Interface Builder

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

}

}

For those of you who are looking for a more updated answer and a cleaner approach than using the NSNotificationCenter, there is a class method on FBSDKProfile that you can call after logging in that looks like:

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

}];

I'm in objc but ran into this problem as well.

First, Johnny Z's answer is correct that you have to explicitly enable the FBSDKProfile class by setting 'enableUpdatesOnAccessTokenChange' to true and then observing changes. In fact, that's what FBSDKProfile is doing internally according to the header comments.

However, as noted by fanfan in a comment, the FBSDKProfile doesn't have all of the fields you might be interested in. I ended up not using FBSDKProfile and just made a graph request for 'me' like so:

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

Note: You can still observe changes with the 'FBSDKProfileDidChangeNotification' notification. Read FBSDKAccessToken.h comments for documentation of this notification and the keys it will send.

In objective-C I used the following approach based on comments in this thread.

If this flag is set to yes, after login or logout a notification will be called. So in viewDidLoad add this line of code and add the notification to be called after user profile get received.

[FBSDKProfile enableUpdatesOnAccessTokenChange:YES];

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

All information in this link https://developers.facebook.com/docs/facebook-login/ios/v2.3#profiles

It seems like you need to call the AppDelegate method to initialize the SDK before you call enableUpdatesOnAccessTokenChange:YES

I made my AppDelegate didFinishLaunching method like this

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

That seemed to work. You still might need to do a /me request if you need items not in the profile object.

Andrei Ogrenich

I had similar problem with [FBSDKProfile currentProfile],but in Objective-C, you can also get user profile's properties from Graph API with response to https://graph.facebook.com/me?access_token= with your currentAccessToken.

Swift 4 version,

FBSDKProfile.loadCurrentProfile(completion: {
        profile, error in
        let url = profile?.imageURL(for: FBSDKProfilePictureMode.square, size:  self.imageview.frame.size)
    })
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!