透過共用 Apple 鑰匙圈啟用跨應用程式驗證機制

如要在 Apple 平台上多個應用程式或擴充功能之間共用驗證狀態,請儲存 使用鑰匙圈服務而共用鑰匙圈的驗證狀態 ,並設定應用程式使用共用鑰匙圈。

這可讓使用者:

  • 登入一次,即可登入所有屬於相同存取權的應用程式 群組。
  • 登出一次,即可登出所有屬於相同存取權的應用程式 群組。

在應用程式之間共用驗證狀態

如要在應用程式之間共用驗證狀態,請按照以下步驟操作:

  1. 為應用程式設定存取權群組。

    您可以使用鑰匙圈存取群組或應用程式群組。請參閱「分享應用程式集合中的鑰匙圈項目存取權」 。

    如要設定鑰匙圈存取群組,請為每個應用程式執行下列操作:

    1. 在 Xcode 中,前往「專案設定」>功能
    2. 啟用「鑰匙圈共用」。
    3. 新增鑰匙圈群組 ID。在所有 用於分享狀態的應用程式
  2. 在每個應用程式中,將存取權群組設為鑰匙圈存取權群組或應用程式群組 您在上一個步驟中建立的名稱

    Swift

    do {
      try Auth.auth().useUserAccessGroup("TEAMID.com.example.group1")
    } catch let error as NSError {
      print("Error changing user access group: %@", error)
    }
    

    Objective-C

    [FIRAuth.auth useUserAccessGroup:@"TEAMID.com.example.group1"
                                       error:nil];
    
  3. 在至少一個應用程式中,以任何方式登入使用者。

    Swift

    Auth.auth().signInAnonymously { result, error in
      // User signed in
    }
    

    Objective-C

    [FIRAuth signInAnonymouslyWithCompletion:^(FIRAuthDataResult *_Nullable result,
                                               NSError *_Nullable error) {
      // User signed in
    }];
    

    存取權群組的所有應用程式中,都能使用同一名使用者。

    Swift

    var user = Auth.auth().currentUser
    

    Objective-C

    FIRUser *user = FIRAuth.auth.currentUser;
    

切換回未共用鑰匙圈

  1. 如要停止共用驗證狀態,請將存取權群組設為「nil」。

    Swift

    do {
      try Auth.auth().useUserAccessGroup(nil)
    } catch let error as NSError {
      print("Error changing user access group: %@", error)
    }
    

    Objective-C

    [FIRAuth.auth useUserAccessGroup:nil error:nil];
    
  2. 透過任何登入方式登入使用者帳戶。系統不會提供使用者狀態 給任何其他應用程式

    Swift

    Auth.auth().signInAnonymously { result, error in
      // User signed in
    }
    

    Objective-C

    [FIRAuth signInAnonymouslyWithCompletion:^(FIRAuthDataResult *_Nullable result,
                                       NSError *_Nullable error) {
      // User signed in
    }];
    

將已登入的使用者遷移至共用鑰匙圈

如何遷移已登入共用狀態的使用者:

  1. 參照目前使用者的參照,供日後使用。

    Swift

    var user = Auth.auth().currentUser
    

    Objective-C

    FIRUser *user = FIRAuth.auth.currentUser;
    
  2. (選用) 檢查您要共用的存取權群組的驗證狀態。

    Swift

    let accessGroup = "TEAMID.com.example.group1"
    var tempUser: User?
    do {
      try tempUser = Auth.auth().getStoredUser(forAccessGroup: accessGroup)
    } catch let error as NSError {
      print("Error getting stored user: %@", error)
    }
    if tempUser != nil {
      // A user exists in the access group
    } else {
      // No user exists in the access group
    }
    

    Objective-C

    NSString *accessGroup = @"TEAMID.com.example.group1";
    FIRUser *tempUser = [FIRAuth getStoredUserForAccessGroup:accessGroup
                                                       error:nil];
    if (tempUser) {
      // A user exists in the access group
      } else {
      // No user exists in the access group
    }
    
  3. 使用先前在專案設定中設定的存取權群組。

    Swift

    do {
      try Auth.auth().useUserAccessGroup(accessGroup)
    } catch let error as NSError {
      print("Error changing user access group: %@", error)
    }
    

    Objective-C

    [FIRAuth.auth useUserAccessGroup:accessGroup error:nil];
    
  4. 更新目前的使用者。

    Swift

    Auth.auth().updateCurrentUser(user!) { error in
      // Error handling
    }
    

    Objective-C

    [FIRAuth.auth updateCurrentUser:user completion:^(NSError * _Nullable error) {
      // Error handling
    }];
    
  5. 使用者現在也能由擁有相同存取權群組的其他應用程式存取。