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

Gou*_*ham 4 iphone facebook objective-c ios 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");
     }
 }];
Run Code Online (Sandbox Code Playgroud)

Nim*_*ekh 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;
}  
Run Code Online (Sandbox Code Playgroud)

但请记住,这不是在IOS 6中触发的.在ios 6中将触发以下方法.

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

return [FBSession.activeSession handleOpenURL:url];
 }
Run Code Online (Sandbox Code Playgroud)

如果会话状态因登录或断开而发生更改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];
}
}
Run Code Online (Sandbox Code Playgroud)