2013-04-24 92 views
1

我正在做第一次FB与iOS SDK集成的尝试。无限的会话与facebook ios sdk FBLoginView

我已经成功地通过FBLoginView与我的fb应用程序建立了连接,该工作到目前为止已经工作。

这里是我相应的调用:

-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView{ 
    NSLog(@"logged in"); 
} 
-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{ 
    NSLog(@"logged out"); 
    [FBSettings setLoggingBehavior:[NSSet setWithObjects:FBLoggingBehaviorFBRequests, nil]]; 
    if (FBSession.activeSession.isOpen) { 
     [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection,id<FBGraphUser> user,NSError *error) { 
     if (!error) { 
      NSString *fbID = user.id; 
      NSLog(@"UserID: %@",fbID); 
      NSLog(@"TESTING: %@",user.name); 
     } 
     }]; 
    } 
} 
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView 
          user:(id<FBGraphUser>)user { 
    NSLog(@"Hello %@!", user.first_name); 
} 
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error { 
    NSLog(@"FBLoginView encountered an error=%@", error); 
} 

我有1小1大(主题)的问题。

:请求被好吧fullfilled和我得到一个不错的日志吧:

Response Body: 
(
     { 
     body =   { 
      gender = male; 
      id = ###; 
      ... 
     }; 
     code = 200; 
    } 
) 

但不知何故,我的2个testlogs不秀,任何想法?

大(主题):我在我的应用程序的指定控制器中建立连接。我的问题是,在应用程序的每次重新启动时,连接以某种方式丢失(会话不再活动)..有没有办法使用FBLoginView建立无限连接?

我已经添加下面的调用&方法来我的appdelegate:

//upon start: 
if (![FBSession activeSession].isOpen) { 
    [self connectWithFacebook]; 
} 


- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI 
{ 
    //.. 
    return [FBSession openActiveSessionWithReadPermissions:permissions 
              allowLoginUI:allowLoginUI 
             completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { 
              if (error) { 
               NSLog (@"Handle error %@", error.localizedDescription); 
              } else { 
               [FBSession setActiveSession:session]; 
               [self checkSessionState:state]; 
              } 
             }]; 
} 
- (void) connectWithFacebook { 

    [self openSessionWithAllowLoginUI:YES]; 
} 

- (void) checkSessionState:(FBSessionState)state { 
    switch (state) { 
     case FBSessionStateOpen: 
     break; 
     case FBSessionStateCreated: 
     break; 
     case FBSessionStateCreatedOpening: 
     break; 
     case FBSessionStateCreatedTokenLoaded: 
     break; 
     case FBSessionStateOpenTokenExtended: 
     // I think this is the state that is calling 
     break; 
     case FBSessionStateClosed: 
     break; 
     case FBSessionStateClosedLoginFailed: 
     break; 
     default: 
     break; 
    } 
} 

这允许重新登录,但每一个应用程序被打开登陆界面时再次表明这是不是很用户友好。有没有解决这个问题的办法,如果是的话,我错过了什么或者我可以继续向哪个方向发展?

回答

4

这是我的实现,保持会话开放60天(最大允许时间FB SDK 3.2.1)

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI 
{ 
    NSArray *readPermissions = @[@"email",@"friends_birthday",@"friends_likes",@"friends_interests",@"user_birthday",@"user_interests",@"user_likes",@"user_location"]; 

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

- (void)sessionStateChanged:(FBSession *)session 
         state:(FBSessionState)state 
         error:(NSError *)error 
{ 
    switch (state) { 
     case FBSessionStateOpen: { 
      // We have a valid session 
      NSLog(@"User session found"); 
      if (FBSession.activeSession.isOpen) { 
       [FBRequestConnection 
       startForMeWithCompletionHandler:^(FBRequestConnection *connection, 
                id<FBGraphUser> user, 
                NSError *error) { 
         if (!error) { 
          NSLog(@"accessToken: %@ userID: %@",[FBSession activeSession].accessTokenData.accessToken,user.id); 
          self.userID = user.id; 
          [[NSUserDefaults standardUserDefaults] setValue:user.first_name forKey:@"first_name"]; 
          [[NSUserDefaults standardUserDefaults] setValue:user.last_name forKey:@"last_name"]; 
          NSString *accessToken = [FBSession activeSession].accessTokenData.accessToken; 
          if (accessToken) { 
           [[NSNotificationCenter defaultCenter] postNotificationName:FacebookLoginInProgressNotification object:nil]; 
           [self submitFacebookUserID:user.id andAccessToken:accessToken]; 
          } 
          else { 
           NSLog(@"no access token for userID: %@",user.id); 
           [[NSNotificationCenter defaultCenter] postNotificationName:FacebookLoginFailureNotification object:nil]; 
          } 
         } 
         else { 
          //handle error retrieving User ID 
          NSLog(@"error retrieving User ID [%@]",[error localizedDescription]); 
          [[NSNotificationCenter defaultCenter] postNotificationName:FacebookLoginFailureNotification object:nil]; 
         } 
       }]; 
      } else { 
       [FBSession setActiveSession:session]; 
      } 

      // Pre-fetch and cache the friends for the friend picker as soon as possible to improve 
      // responsiveness when the user tags their friends. 
      FBCacheDescriptor *cacheDescriptor = [FBFriendPickerViewController cacheDescriptor]; 
      [cacheDescriptor prefetchAndCacheForSession:session]; 
     } 
      break; 
     case FBSessionStateClosed: { 
      [FBSession.activeSession closeAndClearTokenInformation]; 
     } 
      break; 
     case FBSessionStateClosedLoginFailed: { 
      [FBSession.activeSession closeAndClearTokenInformation]; 
     } 
      break; 
     default: 
      break; 
    } 

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

    if (error) { 
     NSLog(@"Facebook Error %@", error); 
    } 
} 
+0

感谢你的洞察力!我有点困惑于如何将它与'FBLoginView'连接起来,还是应该使用常规的'自制'按钮并将其与'openSessionWithAllowUI'连接起来? – 2013-04-25 06:38:52

+0

我使用'自制'按钮,在iOS 6中,如果用户在设置中连接了FB,则会自动连接到FB。在iOS 5中,它抛出了一个登录窗口,只是看到一个统计数据,只有11%仍然在iOS 5上,所以没有什么大不了的 – JeffN 2013-04-25 16:26:25

+0

aaaand接受:)非常感谢您抽出时间:) – 2013-04-26 06:17:31