2013-03-29 5 views
4

이 작업을하는 데 가장 힘든 시간을 보내고 있습니다. IOS 앱이 있는데 Facebook SDK 3.1에 통합하려고합니다. 사용자가 토큰을 캐싱하고 있다면 페이스 북에 로그인하도록 선택할 수 있습니다. 반환 된 토큰 만료 날짜는 몇 주 전이며 로그인/로그 아웃이 제대로 작동하고 전경으로 돌아갑니다. 그러나 앱을 종료 할 때마다 FBSession은 유지되지 않습니다. 응용 프로그램이 다시 시작될 때 응용 프로그램 대리인이 [self openSessionWithAllowLoginUI:NO]을 수행하고 세션을 새로 고치려고 시도하기 때문에이 사실을 알고 있습니다. 그러나 항상 null을 반환합니다. 나는 다른 튜토리얼과 다른 게시물을 따라 왔고 내가 애플 리케이션 델리게이트에서 내가 뭘 잘못하고있는 것처럼 보이지 않는 것 같다.앱을 닫을 때 IOS가 FBSession을 잃습니다.

나는 왜 내가이 세션을 앱을 닫을 때 잃어 버리고 있는지에 대해 벽에 머리를 치고있다. appDelegate를 첨부했습니다.

#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

AppDelegate.m 귀하의 의견 주셔서 모두 감사드립니다. 다행히 나는 마침내 문제를 추적 할 수있었습니다. 무엇 발생되었다 내가 루트 뷰 컨트롤러에서 내 논리를 이동했지만, 여전히 그래서 내가 두 번 연속으로 호출하는 것을 의미

[self openSessionWithAllowLoginUI:NO]

에 전화를했다이었다. appDelegate에 한 번, 루트보기 컨트롤러에 다시 한 번 표시됩니다. 두 번째 호출은 내 토큰을 자동으로 로그 아웃 한 것으로 보입니다. 일단 이것을 확인하고 루트보기 컨트롤러에서 기능을 제거하면 모든 것이 예상대로 작동했습니다.

도움 주셔서 감사합니다.

+0

변경된 코드를 업로드 할 수 있습니까? 부디 –

관련 문제