问题
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?
回答1:
The FBSDKProfile fetch may not have been completed by the time of the login callback. Instead you can observe for a 'FBSDKProfileDidChangeNotification' notification post.
回答2:
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!) {
}
}
回答3:
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) {
}];
回答4:
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.
回答5:
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
回答6:
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.
回答7:
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.
回答8:
Swift 4 version,
FBSDKProfile.loadCurrentProfile(completion: {
profile, error in
let url = profile?.imageURL(for: FBSDKProfilePictureMode.square, size: self.imageview.frame.size)
})
来源:https://stackoverflow.com/questions/29292371/facebook-sdk-v4-0-for-ios-fbsdkprofile-currentprofile-not-being-set