Bật tính năng xác thực trên nhiều ứng dụng bằng Apple Keychain dùng chung
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Để chia sẻ trạng thái xác thực trên nhiều ứng dụng hoặc tiện ích trên các nền tảng của Apple, hãy lưu trữ
trạng thái xác thực trong chuỗi khoá dùng chung bằng Dịch vụ Keychain
và định cấu hình ứng dụng để dùng chuỗi khoá dùng chung.
Điều này cho phép người dùng:
Đăng nhập một lần và đăng nhập trên tất cả các ứng dụng có cùng quyền truy cập
nhóm.
Đăng xuất một lần và đăng xuất khỏi tất cả ứng dụng có cùng quyền truy cập
nhóm.
Chia sẻ trạng thái xác thực giữa các ứng dụng
Cách chia sẻ trạng thái xác thực giữa các ứng dụng:
Thiết lập một nhóm truy cập cho các ứng dụng của bạn.
Trong ít nhất một ứng dụng, hãy đăng nhập người dùng bằng bất kỳ phương thức đăng nhập nào.
Swift
Auth.auth().signInAnonymously{result,errorin// User signed in}
Objective-C
[FIRAuthsignInAnonymouslyWithCompletion:^(FIRAuthDataResult*_Nullableresult,NSError*_Nullableerror){// User signed in}];
Người dùng hiện tại vẫn có thể dùng được trong tất cả các ứng dụng của nhóm truy cập.
Swift
varuser=Auth.auth().currentUser
Objective-C
FIRUser*user=FIRAuth.auth.currentUser;
Chuyển về chuỗi khoá không chia sẻ
Đặt nhóm truy cập thành nil để ngừng chia sẻ trạng thái xác thực.
Swift
do{tryAuth.auth().useUserAccessGroup(nil)}catchleterrorasNSError{print("Error changing user access group: %@",error)}
Objective-C
[FIRAuth.authuseUserAccessGroup:nilerror:nil];
Đăng nhập người dùng bằng bất kỳ phương thức đăng nhập nào. Trạng thái người dùng sẽ không hiển thị
với bất kỳ ứng dụng nào khác.
Swift
Auth.auth().signInAnonymously{result,errorin// User signed in}
Objective-C
[FIRAuthsignInAnonymouslyWithCompletion:^(FIRAuthDataResult*_Nullableresult,NSError*_Nullableerror){// User signed in}];
Di chuyển người dùng đã đăng nhập sang chuỗi khoá dùng chung
Cách di chuyển người dùng đã đăng nhập sang trạng thái dùng chung:
Tham chiếu đến người dùng hiện tại để sử dụng trong tương lai.
Swift
varuser=Auth.auth().currentUser
Objective-C
FIRUser*user=FIRAuth.auth.currentUser;
(Không bắt buộc) Kiểm tra trạng thái xác thực của nhóm truy cập mà bạn muốn chia sẻ.
Swift
letaccessGroup="TEAMID.com.example.group1"vartempUser:User?do{trytempUser=Auth.auth().getStoredUser(forAccessGroup:accessGroup)}catchleterrorasNSError{print("Error getting stored user: %@",error)}iftempUser!=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=[FIRAuthgetStoredUserForAccessGroup:accessGrouperror:nil];if(tempUser){// A user exists in the access group}else{// No user exists in the access group}
Sử dụng một nhóm truy cập mà bạn đã thiết lập trước đây trong phần cài đặt dự án.
Swift
do{tryAuth.auth().useUserAccessGroup(accessGroup)}catchleterrorasNSError{print("Error changing user access group: %@",error)}
[null,null,["Cập nhật lần gần đây nhất: 2025-08-16 UTC."],[],[],null,["To share authentication states across multiple apps or extensions on Apple platforms, store\nthe authentication state in a shared keychain using [Keychain Services](https://developer.apple.com/documentation/security/keychain_services)\nand configure your apps to use the shared keychain.\n\nThis allows users to:\n\n- Sign in once and be signed in across all apps that belong to the same access group.\n- Sign out once and be signed out across all apps that belong to the same access group.\n\n| **Note:** Shared keychain does not automatically update users across apps in real time. If you make a change to a user in one app, the user must be reloaded in any other shared keychain apps before the changes will be visible.\n\nShare auth state between apps\n\nTo share auth state between apps:\n\n1. Set up an access group for your apps.\n\n You can use either a keychain access group or an app group. See [Sharing Access to Keychain Items Among a Collection of Apps](https://developer.apple.com/documentation/security/keychain_services/keychain_items/sharing_access_to_keychain_items_among_a_collection_of_apps)\n for details.\n\n To set up a keychain access group, do the following for each app:\n 1. In Xcode, go to **Project settings \\\u003e Capabilities**.\n 2. Enable Keychain Sharing.\n 3. Add a keychain group identifier. Use the same identifier for all of the apps you want to share state.\n2. In each app, set the access group to the keychain access group or app group\n you created in the previous step.\n\n Swift \n\n do {\n try Auth.auth().useUserAccessGroup(\"TEAMID.com.example.group1\")\n } catch let error as NSError {\n print(\"Error changing user access group: %@\", error)\n }\n\n Objective-C \n\n [FIRAuth.auth useUserAccessGroup:@\"TEAMID.com.example.group1\"\n error:nil];\n\n3. In at least one app, sign in a user with any sign in method.\n\n Swift \n\n Auth.auth().signInAnonymously { result, error in\n // User signed in\n }\n\n Objective-C \n\n [FIRAuth signInAnonymouslyWithCompletion:^(FIRAuthDataResult *_Nullable result,\n NSError *_Nullable error) {\n // User signed in\n }];\n\n The same current user is available in all apps in the access group. \n\n Swift \n\n var user = Auth.auth().currentUser\n\n Objective-C \n\n FIRUser *user = FIRAuth.auth.currentUser;\n\nSwitch back to an unshared keychain\n\n1. Set the access group to `nil` to stop sharing auth state.\n\n Swift \n\n do {\n try Auth.auth().useUserAccessGroup(nil)\n } catch let error as NSError {\n print(\"Error changing user access group: %@\", error)\n }\n\n Objective-C \n\n [FIRAuth.auth useUserAccessGroup:nil error:nil];\n\n2. Sign in a user with any sign in method. The user state will not be available\n to any other apps.\n\n Swift \n\n Auth.auth().signInAnonymously { result, error in\n // User signed in\n }\n\n Objective-C \n\n [FIRAuth signInAnonymouslyWithCompletion:^(FIRAuthDataResult *_Nullable result,\n NSError *_Nullable error) {\n // User signed in\n }];\n\nMigrate a signed-in user to a shared keychain\n\nTo migrate a user who's already signed in to a shared state:\n\n1. Make a reference to the current user for future use.\n\n Swift \n\n var user = Auth.auth().currentUser\n\n Objective-C \n\n FIRUser *user = FIRAuth.auth.currentUser;\n\n | **Note:** Switching from non-sharing to a shared keychain results in clearing the signed-in user. Switching from a shared to unshared keychain will not clear the signed-in user, even when no app uses that access group.\n2. (Optional) Check the auth state of the access group you want to share.\n\n Swift \n\n let accessGroup = \"TEAMID.com.example.group1\"\n var tempUser: User?\n do {\n try tempUser = Auth.auth().getStoredUser(forAccessGroup: accessGroup)\n } catch let error as NSError {\n print(\"Error getting stored user: %@\", error)\n }\n if tempUser != nil {\n // A user exists in the access group\n } else {\n // No user exists in the access group\n }\n\n Objective-C \n\n NSString *accessGroup = @\"TEAMID.com.example.group1\";\n FIRUser *tempUser = [FIRAuth getStoredUserForAccessGroup:accessGroup\n error:nil];\n if (tempUser) {\n // A user exists in the access group\n } else {\n // No user exists in the access group\n }\n\n3. Use an access group that you previously set in the project settings.\n\n Swift \n\n do {\n try Auth.auth().useUserAccessGroup(accessGroup)\n } catch let error as NSError {\n print(\"Error changing user access group: %@\", error)\n }\n\n Objective-C \n\n [FIRAuth.auth useUserAccessGroup:accessGroup error:nil];\n\n4. Update the current user.\n\n Swift \n\n Auth.auth().updateCurrentUser(user!) { error in\n // Error handling\n }\n\n Objective-C \n\n [FIRAuth.auth updateCurrentUser:user completion:^(NSError * _Nullable error) {\n // Error handling\n }];\n\n | **Note:** You will receive 2 notifications if you are listening to auth state changes on FIRAuth: first, it is triggered with nil (when switching to a new access group), and then triggered again with user (when `updateCurrentUser` is called). You can ignore these notifications.\n5. The user now can be accessed by other apps that have access to the same access group."]]