使用共用 Apple 鑰匙圈啟用跨應用程式身份驗證

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

這允許用戶:

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

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

若要在應用程式之間共用身份驗證狀態:

  1. 為您的應用程式設定訪問群組。

    您可以使用鑰匙串存取群組或應用程式群組。有關詳細信息,請參閱在應用程式集合中共享對鑰匙串項目的存取

    若要設定鑰匙串存取群組,請對每個應用程式執行以下操作:

    1. 在 Xcode 中,前往專案設定 > 功能
    2. 啟用鑰匙串共享。
    3. 新增鑰匙串組標識符。對要共享狀態的所有應用程式使用相同的識別碼。
  2. 在每個應用程式中,將存取群組設定為您在上一個步驟中建立的鑰匙圈存取群組或應用程式群組。

    迅速

    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. 在至少一個應用程式中,使用任意登入方法登入使用者。

    迅速

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

    Objective-C

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

    同一目前使用者可在存取群組中的所有應用程式中使用。

    迅速

    var user = Auth.auth().currentUser
    

    Objective-C

    FIRUser *user = FIRAuth.auth.currentUser;
    

切換回非共享鑰匙串

  1. 將存取群組設定為nil以停止共用身份驗證狀態。

    迅速

    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. 使用任何登入方法登入使用者。用戶狀態將不可用於任何其他應用程式。

    迅速

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

    Objective-C

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

將登入使用者遷移到共享鑰匙串

若要將已登入的使用者移轉到共用狀態:

  1. 為目前使用者提供參考以供日後使用。

    迅速

    var user = Auth.auth().currentUser
    

    Objective-C

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

    迅速

    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. 使用您先前在專案設定中設定的存取群組。

    迅速

    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. 更新目前使用者。

    迅速

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

    Objective-C

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