4

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

我正在使用此代码

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");
     }
 }];
4

1 回答 1

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];
}
}
于 2013-01-23T05:16:05.540 回答