2013-01-23 104 views
4

在我的iPhone應用程序重定向到Facebook登錄像Facebook應用程序點擊一個按鈕時。再次登錄後重定向到我的應用程序。如何從Facebook登錄重定向到iPhone應用程序登錄

我使用這個代碼

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

回答

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.In觸發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]; 
} 
} 
+0

下面是使用共享工具包http://goo.gl/S2Lh9的FB連接的完整教程。 –