2012-11-05 44 views
7

如果在調用closeAndClearTokenInformation之後調用openWithBehavior,則會導致EXC_BAD_ACCESS。無論是使用本機iOS內置對話框還是其中一個快速切換對話框。Facebook iOS SDK 3.1在隨後調用FBSession時崩潰openWithBehavior

我們的代碼通過作品登錄到FB,第一次:

if (![FBSession activeSession]) { 
    #ifdef FREE_APP 
     NSString* suffix = @"free"; 
    #else 
     NSString* suffix = @"paid"; 
    #endif 
    FBSession *session = [[[FBSession alloc] initWithAppID:@"111111111111111" 
          permissions:permissions 
         urlSchemeSuffix:suffix 
        tokenCacheStrategy:nil] autorelease]; 
    [FBSession setActiveSession:session]; 
} 
else if ([FBSession activeSession].isOpen) 
    [[FBSession activeSession] close]; 

[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent 
       completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { 
            [self sessionStateChanged:session state:state error:error]; 
           }]; 

我們的代碼註銷,之後,上面的代碼openWithBehavior失敗後:

[[FBSession activeSession] closeAndClearTokenInformation]; 

我最初使用openActiveSessionWithReadPermissions代替的openWithBehavior,如3.1文檔中所述,它不會崩潰,但從FB應用程序/ Safari切換回來的應用程序不起作用。也許是因爲需要有後綴?如果修復應用切換並回到最簡單的方式,請告知。

謝謝。

回答

7

當我在5.x的模擬器跑了,我看到了一個額外的,非常有幫助的,錯誤的openWithBehavior消息,然後看着它在這使得事情更加清楚來源:

if (!(self.state == FBSessionStateCreated || 
     self.state == FBSessionStateCreatedTokenLoaded)) { 
    // login may only be called once, and only from one of the two initial states 
    [[NSException exceptionWithName:FBInvalidOperationException 
          reason:@"FBSession: an attempt was made to open an already opened or closed session" 
          userInfo:nil] 
    raise]; 
} 

我我改變了我的代碼,在調用openWithBehavior之前總是創建一個全新的會話,並且看起來很開心。

UPDATE: 下面是更新後的代碼,檢查活動會話,然後將其關閉,總是實例化一個新的會話之前...

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI { 


     if ([FBSession activeSession]) 
     [[FBSession activeSession] closeAndClearTokenInformation]; 

     #ifdef FREE_APP 
     NSString* suffix = @"free"; 
     #else 
     NSString* suffix = @"paid"; 
     #endif 

     NSArray *permissions = [[NSArray alloc] initWithObjects:@"email", nil]; 

     FBSession *session = [[FBSession alloc] initWithAppID:mFacebookID 
               permissions:permissions 
              urlSchemeSuffix:suffix 
             tokenCacheStrategy:nil]; 

     [FBSession setActiveSession:session]; 

     If (allowLoginUI == YES) { 
     NSLog(@"Calling openWithBehavior"); 
     [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent 
            completionHandler:^(FBSession *session, FBSessionState state, NSError *error) 
            { 
             [self sessionStateChanged:session state:state error:error]; 
            } 
     ]; 
    } else if(session.state == FBSessionStateCreatedTokenLoaded) { 
     NSLog(@"Calling openWith completion handler"); 
     [session openWithCompletionHandler:^(FBSession *_session, FBSessionState status, NSError *error) 
              { [self sessionStateChanged:session state:status error:error];} 
     ]; 
    } 

    [session release]; 

    return true; 
    } 
+0

,我沒有得到在哪裏,以及爲什麼寫這個代碼。並且我在openWithBehaviour之前編寫它仍然在openWithBehaviour中崩潰 – Heena

+0

上面的第一個片段是來自發布錯誤的Facebook類。我只是把它包括在內,以增加發生的事情。在繼續創建新會話之前,我添加了對活動會話的檢查並關閉它之後添加了我的代碼。 – leontx