2013-03-29 79 views
4

我最難得到這個工作。我有一個IOS應用程序,我試圖集成到Facebook SDK 3.1。如果用戶正在緩存令牌,用戶可以選擇登錄Facebook。返回的令牌過期日期是提前幾周,並且我有一切正常工作,登錄/註銷,返回到前臺。但是,每次關閉應用程序FBSession都未被保留。我知道這是因爲當應用程序重新啓動時,應用程序委託執行[self openSessionWithAllowLoginUI:NO]並嘗試刷新會話,但這總是返回null。我跟着其他教程和其他帖子,似乎無法看到我在我的應用程序委託中做錯了什麼。iOS在關閉應用程序時丟失FBSession

爲什麼我在應用程序關閉時失去了本次會話,我正在撞牆。我已附加了我的appDelegate。

AppDelegate.m

#import "AppDelegate.h" 
@implementation AppDelegate 

NSString *const [email protected]"ro.Tag:FBSessionStateChangedNotification"; 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 

    [self openSessionWithAllowLoginUI:NO]; 
    NSLog(@"%@",[[FBSession activeSession] accessToken]); 

    return YES; 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 

    // We need to properly handle activation of the application with regards to SSO 
    // (e.g., returning from iOS 6.0 authorization dialog or from fast app switching). 
    [FBSession.activeSession handleDidBecomeActive]; 
} 

/* 
* Callback for session changes. 
*/ 
- (void)sessionStateChanged:(FBSession *)session 
         state:(FBSessionState) state 
         error:(NSError *)error 
{ 
    switch (state) { 
     case FBSessionStateOpen: 
      if (!error) { 
       // We have a valid session 
       NSLog(@"User session found"); 
       NSLog(@"session %@",session); 
      } 
      break; 
     case FBSessionStateClosed: 
     case FBSessionStateClosedLoginFailed: 
      [FBSession.activeSession closeAndClearTokenInformation]; 
      break; 
     default: 
      break; 
    } 

    [[NSNotificationCenter defaultCenter] 
    postNotificationName:FBSessionStateChangedNotification 
    object:session]; 

    if (error) { 
     UIAlertView *alertView = [[UIAlertView alloc] 
            initWithTitle:@"Error" 
            message:error.localizedDescription 
            delegate:nil 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 
     [alertView show]; 
    } 
} 

/* 
* Opens a Facebook session and optionally shows the login UX. 
*/ 
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI { 
    NSArray *permissions = [[NSArray alloc] initWithObjects: 
          @"email", 
          @"user_games_activity", 
          @"user_location", 
          @"user_likes", 
          @"user_birthday", 
          nil]; 

    //NSLog(@"permissions: %@",permissions); 

    return [FBSession openActiveSessionWithReadPermissions:permissions 
               allowLoginUI:allowLoginUI 
             completionHandler:^(FBSession *session, 
                  FBSessionState state, 
                  NSError *error) { 
              [self sessionStateChanged:session 
                   state:state 
                   error:error]; 
             }]; 
} 

/* 
* If we have a valid session at the time of openURL call, we handle 
* Facebook transitions by passing the url argument to handleOpenURL 
*/ 
- (BOOL)application:(UIApplication *)application 
      openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication 
     annotation:(id)annotation { 
    // attempt to extract a token from the url 
    return [FBSession.activeSession handleOpenURL:url]; 
} 
/* 
    *Logout 
    * 
*/ 
- (void) closeSession { 
    [FBSession.activeSession closeAndClearTokenInformation]; 
} 
@end 
+0

您是否嘗試過在openSessionWithAllowLoginUI中打印錯誤。可能是你得到錯誤。 – meth

+0

嘗試使用Facebook iOS SDK 3.2(.1),然後再進一步追究:3.2更新對令牌保存和其他認證流程問題進行了修復。 – cbowns

回答

3

謝謝兩位的意見。幸運的是,我終於找到了這個問題。什麼正在發生的是,我有根視圖控制器將我的邏輯,但仍然有一個電話

[self openSessionWithAllowLoginUI:NO]

因此,這意味着我連續兩次調用它。一旦進入appDelegate並再次在根視圖控制器中。第二個電話似乎清除我的令牌自動登出我。一旦我能夠識別這個並從我的根視圖控制器中刪除功能,一切都按預期工作。

感謝您的幫助。

+0

你可以上傳你更改的代碼嗎?請 –

相關問題