簡體   English   中英

一台設備上的多個用戶可在iOS應用中使用Facebook登錄

[英]Facebook login in iOS app for multiple users on one device

我的應用程序將被多個用戶使用。

如何以某種方式集成Facebook,以使Safari在每次有人嘗試登錄時都不會保存任何用戶憑據並拋出登錄頁面。

我已經使用FBSessionLoginBehaviorForcingWebView強制在應用程序中進行Web視圖登錄,但是問題是,當第一個用戶嘗試通過應用程序中提供的視圖登錄到fb時,控件隨后轉到了safari進行身份驗證,並且只保存了信譽。

因此,當其他用戶嘗試登錄並在應用程序本地UIWebView中輸入其憑據時,該控件將再次轉到safari(已經存儲了previos憑據)且新用戶無法進行身份驗證。

因此,基本上第一個用戶是永久登錄的。清除Web視圖的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];
}
}

另外,使用您的方法修改應用程序委托。

找到了解決方案。 基本上,調用以下命令:

[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];

它不是很優雅,但可以。 現在,當下一個用戶嘗試登錄時,它將強制他們輸入其憑據。

要關閉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共享按鈕時,檢查該會話是否存在。 如果不是,則調用openSession方法。 它將打開Facebook登錄視圖。

Facebook Github存儲庫中有一個示例 ,顯示了如何在多個用戶之間切換。

看起來它需要使用自定義登錄流程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM