我正在尝试将登录与FB合并到我的应用程序中。
我面临的问题是,如果设备上未安装FB应用程序,则FB登录成功。 但是,如果设备上安装了FB应用程序,则会话不会打开。
我正在使用Facebook SDK 3.13,而我用于登录活动的代码如下:
- (IBAction)buttonTouched:(id)sender
{
[FBSession.activeSession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession setActiveSession:nil];
self.please_wait_view.hidden=NO;
// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
// Close the session and remove the access token from the cache
// The session state handler (in the app delegate) will be called automatically
[FBSession.activeSession closeAndClearTokenInformation];
// If the session state is not any of the two "open" states when the button is clicked
} else {
// Open a session showing the user the login UI
// You must ALWAYS ask for basic_info permissions when opening a session
[FBSession openActiveSessionWithReadPermissions:@[@"basic_info", @"email", @"user_likes",@"user_friends"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
//NSLog(@"%@",[session accessTokenData]);
fb_access_token = [NSString stringWithFormat:@"%@",[session accessTokenData]];
[self sessionStateChanged:session state:state error:error];
}];
}
} 这是会话状态更改的方法:
// This method will handle ALL the session state changes in the app
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
// If the session was opened successfully
if (!error && state == FBSessionStateOpen){
NSLog(@"Session opened");
/ / Show the user the logged-in UI
//[self userLoggedIn];
return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
// If the session is closed
NSLog(@"Session closed");
// Show the user the logged-out UI
//[self userLoggedOut];
}
// Handle errors
if (error){
NSLog(@"Error");
NSString *alertText;
NSString *alertTitle;
// If the error requires people using an app to make an action outside of the app in order to recover
if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
alertTitle = @"Something went wrong";
alertText = [FBErrorUtility userMessageForError:error];
NSLog(@"%@",alertText);
} else {
// If the user cancelled login, do nothing
if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(@"User cancelled login");
// Handle session closures that happen outside of the app
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
alertTitle = @"Session Error";
alertText = @"Your current session is no longer valid. Please log in again.";
//[self showMessage:alertText withTitle:alertTitle];
NSLog(@"%@",alertText);
// Here we will handle all other errors with a generic error message.
// We recommend you check our Handling Errors guide for more information
// https://developers.facebook.com/docs/ios/errors/
} else {
//Get more error information from the error
NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"];
// Show the user an error message
alertTitle = @"Something went wrong";
alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]];
//[self showMessage:alertText withTitle:alertTitle];
NSLog(@"%@",alertText);
}
}
// Clear this token
[FBSession.activeSession closeAndClearTokenInformation];
// Show the user the logged-out UI
//[self userLoggedOut];
}
}
谢谢!
答案 0 :(得分:0)
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [FBSession.activeSession handleOpenURL:url];
}
尝试这个希望这会帮助你
答案 1 :(得分:0)
好的,所以经过一天半的浪费......我终于想出了一个解决方案。
我刚刚更改了我用于FB登录的应用程序,并替换了新的fb应用程序ID和bundleID以及.plist文件中的url方案,它似乎工作正常。 Dunno导致这个问题的原因虽然导致我以前用于登录的应用程序几乎与新的相同!