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

前端 未结 8 2489
星月不相逢
星月不相逢 2020-12-25 13:35

I\'ve just \"upgraded\" my Facebook SDK to 4.0 for my iOS app.

I\'ve got the log in working okay, however, according to the documentation, I\'m now supposed to use

相关标签:
8条回答
  • 2020-12-25 14:02

    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

    0 讨论(0)
  • 2020-12-25 14:08

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

    0 讨论(0)
  • 2020-12-25 14:18

    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) {
    
    }];
    
    0 讨论(0)
  • 2020-12-25 14:18

    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.

    0 讨论(0)
  • 2020-12-25 14:22

    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.

    0 讨论(0)
  • 2020-12-25 14:24

    Swift 4 version,

    FBSDKProfile.loadCurrentProfile(completion: {
            profile, error in
            let url = profile?.imageURL(for: FBSDKProfilePictureMode.square, size:  self.imageview.frame.size)
        })
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题