您可以使用 Firebase 身份驗證來創建和使用臨時匿名帳戶來通過 Firebase 進行身份驗證。這些臨時匿名帳戶可用於允許尚未註冊您的應用的用戶使用受安全規則保護的數據。如果匿名用戶決定註冊您的應用程序,您可以將他們的登錄憑據鏈接到匿名帳戶,以便他們可以在以後的會話中繼續使用受保護的數據。
在你開始之前
使用 Swift Package Manager 安裝和管理 Firebase 依賴項。
- 在 Xcode 中,打開您的應用項目,導航到File > Add Packages 。
- 出現提示時,添加 Firebase Apple 平台 SDK 存儲庫:
- 選擇 Firebase 身份驗證庫。
- 完成後,Xcode 將在後台自動開始解析和下載您的依賴項。
https://github.com/firebase/firebase-ios-sdk
接下來,執行一些配置步驟:
- 如果您尚未將應用連接到 Firebase 項目,請從Firebase 控制台執行此操作。
- 啟用匿名身份驗證:
- 在Firebase 控制台中,打開Auth部分。
- 在登錄方法頁面上,啟用匿名登錄方法。
匿名使用 Firebase 進行身份驗證
當已註銷的用戶使用需要通過 Firebase 進行身份驗證的應用功能時,請完成以下步驟以匿名方式登錄該用戶:
- 在您的
UIApplicationDelegate
中導入FirebaseCore
模塊,以及您的應用委託使用的任何其他Firebase 模塊。例如,要使用 Cloud Firestore 和身份驗證:迅速
import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Objective-C
@import FirebaseCore; @import FirebaseFirestore; @import FirebaseAuth; // ...
- 配置
FirebaseApp
共享實例,通常在您應用的application:didFinishLaunchingWithOptions:
方法中:迅速
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- 調用
signInAnonymouslyWithCompletion:
方法:迅速
Auth.auth().signInAnonymously { authResult, error in // ... }
Objective-C
[[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { // ... }];
- 如果
signInAnonymouslyWithCompletion:
方法完成且沒有錯誤,您可以從FIRAuthDataResult
對像中獲取匿名用戶的帳戶數據:迅速
guard let user = authResult?.user else { return } let isAnonymous = user.isAnonymous // true let uid = user.uid
Objective-C
FIRUser *user = authResult.user; BOOL isAnonymous = user.anonymous; // YES NSString *uid = user.uid;
將匿名帳戶轉換為永久帳戶
當匿名用戶註冊您的應用程序時,您可能希望允許他們使用新帳戶繼續工作——例如,您可能希望讓用戶在註冊之前添加到購物車中的商品在他們的新帳戶中可用帳戶的購物車。為此,請完成以下步驟:
- 當用戶註冊時,完成用戶身份驗證提供程序的登錄流程,直至調用
FIRAuth.signInWith
方法之一,但不包括。例如,獲取用戶的 Google ID 令牌、Facebook 訪問令牌或電子郵件地址和密碼。 獲取新身份驗證提供程序的
FIRAuthCredential
:谷歌登錄
迅速
guard let authentication = user?.authentication, let idToken = authentication.idToken else { return } let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: authentication.accessToken)
Objective-C
GIDAuthentication *authentication = user.authentication; FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken accessToken:authentication.accessToken];
Facebook登入
迅速
let credential = FacebookAuthProvider .credential(withAccessToken: AccessToken.current!.tokenString)
Objective-C
FIRAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken:[FBSDKAccessToken currentAccessToken].tokenString];
電子郵件密碼登錄
迅速
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
Objective-C
FIRAuthCredential *credential = [FIREmailAuthProvider credentialWithEmail:email password:password];
將
FIRAuthCredential
對像傳遞給登錄用戶的linkWithCredential:completion:
方法:迅速
user.link(with: credential) { authResult, error in // ... } }
Objective-C
[[FIRAuth auth].currentUser linkWithCredential:credential completion:^(FIRAuthDataResult *result, NSError *_Nullable error) { // ... }];
如果對linkWithCredential:completion:
的調用成功,則用戶的新帳戶可以訪問匿名帳戶的 Firebase 數據。
下一步
現在用戶可以使用 Firebase 進行身份驗證,您可以使用Firebase 規則控制他們對 Firebase 數據庫中數據的訪問。