Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 FBSDKProfile.currentProfile() to access profile information.

However, this value always seems to be nil, even though my profile picture is being displayed (through a FBSDKProfilePictureView) and my FBSDKAccessToken.currentAccessToken() is not nil.

This is my login 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!) {
    }
}

Any ideas?

like image 685
fanfan Avatar asked Mar 27 '15 02:03

fanfan


4 Answers

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

}

}

like image 103
Johnny Z Avatar answered Oct 22 '22 11:10

Johnny Z


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

}];
like image 28
Eric Alford Avatar answered Oct 22 '22 09:10

Eric Alford


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

like image 21
Chris Pan Avatar answered Oct 22 '22 11:10

Chris Pan


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.

like image 23
Chad Pavliska Avatar answered Oct 22 '22 09:10

Chad Pavliska