Facebook在iOS应用中登录一台设备上的多个用户

时间:2013-09-16 11:45:40

标签: iphone ios facebook ipad ios6

我的应用将由多个用户使用。

如何以某种方式集成Facebook,以便Safari不会保存任何用户凭据和每次有人试图登录时都会抛出一个登录页面。

我已经使用FBSessionLoginBehaviorForcingWebView强制在应用程序内登录Web视图,但问题是,当第一个用户尝试通过应用程序中显示的视图登录到fb时,控件然后进入safari进行身份验证&它只是挽救了信誉。

所以当其他用户尝试登录时,&在应用程序本地UIWebView中输入他的凭据,控件再次进入safari,已经存储了已存在的信誉。新用户无法进行身份验证。

所以基本上第一个用户是永久登录的。清除网页视图的cookie不起作用。我不能清除他们的野生动物园。

帮助 提前谢谢。

这就是我一直在做的事情

if([FBSession activeSession].isOpen)
{
   [[FBSession activeSession] closeAndClearTokenInformation];//Also, clearing all the local cookies
}
else
{
  AppDelegate *delegate = [UIApplication sharedApplication].delegate;
  delegate.session = [[FBSession alloc] init];
  [FBSession setActiveSession:delegate.session];
  [delegate.session openWithBehavior:FBSessionLoginBehaviorForcingWebView    completionHandler:^(FBSession *session1, FBSessionState status, NSError *error) {
    if (!error) {
       [self openSession]; // your method
    }
   }];
 }


- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error{
switch (state) {
    case FBSessionStateOpen:
    {
        [FBRequestConnection startForPostStatusUpdate:@"Some message"completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            if (!error) {

                //logout again
                NSLog(@"startForPostStatusUpdate SUCESS");
            }
            else {
                //logout again
                NSLog(@"startForPostStatusUpdate FAIL");
            }
        }];

    }
        break;
    case FBSessionStateClosed:
    {
        NSLog(@"FBSessionStateClosed");
    }
        break;
    case FBSessionStateClosedLoginFailed:
        [FBSession.activeSession closeAndClearTokenInformation];
        break;
    default:
        break;
}
if (error) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}
}

此外,使用您的方法修改了应用委托。

3 个答案:

答案 0 :(得分:1)

找到解决方案。基本上,请致电以下内容:

[FBSession.activeSession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession setActiveSession:nil];

仅清除本地FB会话信息,但不清除Safari cookie。因此,在我登录用户后,我清除了Safari cookie:

NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]) {
    [storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];

它不是很优雅,但它有效。现在,当下一个用户尝试登录时,它会强制他们输入凭据。

答案 1 :(得分:0)

您只需要在关闭FB会话后使用此方法。

[FBSession.activeSession closeAndClearTokenInformation];

这将清除Facebook令牌,每当按下Facebook按钮的按钮时,它将要求新的令牌凭证,它将再次显示Facebook登录视图。

在AppDelegate中使用以下代码

    // This method will handle facebook session.
    - (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
    {
    // Facebook SDK * login flow *
    // Attempt to handle URLs to complete any auth (e.g., SSO) flow.
    return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication fallbackHandler:^(FBAppCall *call) {
        // Facebook SDK * App Linking *
        // For simplicity, this sample will ignore the link if the session is already
        // open but a more advanced app could support features like user switching.
        if (call.accessTokenData) {
            if ([FBSession activeSession].isOpen) {
                NSLog(@"INFO: Ignoring app link because current session is open.");
            }
            else {
                [self handleAppLink:call.accessTokenData];
            }
        }
    }];
}


// Helper method to wrap logic for handling app links.
- (void)handleAppLink:(FBAccessTokenData *)appLinkToken {
    // Initialize a new blank session instance...
    FBSession *appLinkSession = [[FBSession alloc] initWithAppID:nil
                                                     permissions:nil
                                                 defaultAudience:FBSessionDefaultAudienceNone
                                                 urlSchemeSuffix:nil
                                              tokenCacheStrategy:[FBSessionTokenCachingStrategy nullCacheInstance] ];
    [FBSession setActiveSession:appLinkSession];
    // ... and open it from the App Link's Token.
    [appLinkSession openFromAccessTokenData:appLinkToken
                          completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                              // Forward any errors to the FBLoginView delegate.
                              if (error) {
                                  //TODO: Show error here
                              }
                          }];
}

在您打开Facebook会话的地方添加以下代码

/***********************************************************************
 This method opens browser or App Url to FB authentication and change FB
 session state
 ************************************************************************/
- (void)openSession
{
    NSArray *permisssions = [[NSArray alloc] initWithObjects:@"email",@"user_photos",nil];
    [FBSession openActiveSessionWithReadPermissions:permisssions
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         [self sessionStateChanged:session state:state error:error];
     }];
}




/***********************************************************************
 This method will be called whenever FB Session state changed
 It also shows an error message if any error occurs
 ************************************************************************/
- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen: {
            NSLog(@"FBSession Open State");
            //Enter your code what you want to perform when session is open
        }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            // Once the user has logged in, we want them to
            // be looking at the root view.
            NSLog(@"FBSession State Closed");

             [FBSession.activeSession closeAndClearTokenInformation];
             [FBSession.activeSession close];
             [FBSession setActiveSession:nil];
            break;
        default:
            break;
    }

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

现在,当用户点击Facebook Share按钮时,检查该会话是否存在。如果没有,则调用openSession方法。它将打开facebook登录视图。

答案 2 :(得分:0)

sample in the Facebook Github repository,显示如何在多个用户之间切换。

看起来它需要使用自定义登录流程。