如何从Facebook登录重定向到iPhone应用程序

时间:2013-01-23 05:04:51

标签: iphone ios objective-c facebook ios5

在我的iphone应用程序中,单击按钮时重定向到facebook登录,如facebook app。登录后再次重定向到我的应用程序。

我正在使用此代码

NSArray *permissions =
[NSArray arrayWithObjects:@"user_photos", @"friends_photos",@"email", nil];

[FBSession openActiveSessionWithReadPermissions:permissions
                                   allowLoginUI:YES
                              completionHandler:
 ^(FBSession *session,
   FBSessionState state, NSError *error) {

     if(!error)
     {
         NSLog(@" hi im sucessfully lloged in");
     }
 }];

1 个答案:

答案 0 :(得分:5)

在你的AppDelegate中修改方法

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url  
{

NSString *urlString = [url absoluteString];

if ([urlString hasPrefix:@"fb://xxxxxxxxxxxx"]) {
    [FBSession.activeSession handleOpenURL:url];
    returnValue = YES;
}

return returnValue;
}  

但请记住,这不会在IOS 6中触发。在ios 6中,将触发以下方法。

 - (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {

return [FBSession.activeSession handleOpenURL:url];
 }

如果您的会话状态因登录或断开而发生更改FBsession会调用以下方法,您应该处理您的案例。

- (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState)state
                  error:(NSError *)error {
switch (state) {
    case FBSessionStateOpen: {
        //update permissionsArrat
        [self retrieveUSerPermissions];

        if (!needstoReopenOldSession) {
            //First User information
            [self getUserInformation:nil];
        }

        NSNotification *authorizationNotification = [NSNotification notificationWithName:facebookAuthorizationNotification object:nil];
        [[NSNotificationCenter defaultCenter] postNotification:authorizationNotification];

    }
    case FBSessionStateClosed: {
        break;
    }
    case FBSessionStateClosedLoginFailed: {
        [FBSession.activeSession closeAndClearTokenInformation];
        break;
    }
    default:
        break;
}

if (error) {
    NSNotification *authorizationNotification = [NSNotification notificationWithName:faceBookErrorOccuredNotification object:nil];
    [[NSNotificationCenter defaultCenter] postNotification:authorizationNotification];
}
}