I am trying to migrate my app from Facebook SDK 3.4 to 4.x. I used the following import
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FBSDKLoginKit/FBSDKLoginButton.h>
I used to have two methods
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
I can't find these methods in 4.x SDK.
I can see FBLoginView changed to FBSDKLoginButtonDelegate. But what is the equivalent of above two methods. I tried looking at Facebook iOS samples but could not figure it out.
There's a method called loginButton:didCompleteWithResult:error:
defined in FBSDKLoginButtonDelegate :
- (void) loginButton:(FBSDKLoginButton *)loginButton
didCompleteWithResult: (FBSDKLoginManagerLoginResult *)result
error: (NSError *)error;
Source : Facebook SDK Docs
Note : The above delegate is only for a login button, ie the delegate will be called only when the user performs a login using the login button. So this delegate will not get called every time the app is launched like loginViewShowingLoggedInUser:
used to do.
You have to use FBSDKLoginManager to check if user is aleady logged in :
FBSDKLoginManager
works directly with[FBSDKAccessToken currentAccessToken]
and sets thecurrentAccessToken
upon successful authorizations (or setsnil
in case of logOut).You should check
[FBSDKAccessToken currentAccessToken]
before calling logIn* to see if there is a cached token available (typically in yourviewDidLoad
).
As for retrieving user info, the new SDK does not automatically fetch user info. inside you login success handler, you have to make a graph request :
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:@"id,name,email,gender" forKey:@"fields"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *userID = [[FBSDKAccessToken currentAccessToken] userID];
NSString *userName = [result valueForKey:@"name"];
NSString *emailid=[result valueForKey:@"email"];
NSString *gender=[result valueForKey:@"gender"];
NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [[FBSDKAccessToken currentAccessToken] userID]];
}
else{
NSLog(@"%@",error.localizedDescription);
}
}];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.