您可以使用 Firebase 驗證來建立暫時匿名帳戶,並使用 Firebase 進行驗證。這些臨時匿名帳戶可用於讓尚未註冊您應用程式的使用者,使用受安全性規則保護的資料。如果匿名使用者決定註冊您的應用程式,您可以將他們的登入憑證連結至匿名帳戶,方便他們在日後的工作階段中繼續使用已保護的資料。
事前準備
-
使用 Swift Package Manager 安裝及管理 Firebase 依附元件。
- 在 Xcode 中保持開啟應用程式專案,然後依序點選「File」>「Add Packages」。
- 在系統提示時,新增 Firebase Apple 平台 SDK 存放區:
- 選擇 Firebase 驗證資料庫。
- 在目標建構設定的「Other Linker Flags」部分中新增
-ObjC
標記。 - 完成後,Xcode 會自動開始在背景解析並下載依附元件。
https://github.com/firebase/firebase-ios-sdk.git
- 如果您尚未將應用程式連結至 Firebase 專案,請透過 Firebase 控制台進行。
- 啟用匿名驗證功能:
- 在 Firebase 控制台開啟「驗證」專區。
- 在「Sign-in Methods」頁面中,啟用「Anonymous」登入方式。
- 選用:如果您已將專案升級為透過 Identity Platform 使用 Firebase 驗證,可以啟用自動清理功能。啟用這項設定後,系統會自動刪除 30 天前的匿名帳戶。如果專案已啟用自動清理功能,匿名驗證就不會計入用量限製或帳單配額。請參閱自動清除一節。
透過 Firebase 匿名驗證
當登出的使用者使用需要透過 Firebase 進行驗證的應用程式功能時,請完成下列步驟,以匿名的方式登入使用者:
- 將
FirebaseCore
模組匯入UIApplicationDelegate
,以及應用程式委派使用的任何其他 Firebase 模組。例如,如要使用 Cloud Firestore 和驗證:SwiftUI
import SwiftUI import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Swift
import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Objective-C
@import FirebaseCore; @import FirebaseFirestore; @import FirebaseAuth; // ...
- 在應用程式委派的
application(_:didFinishLaunchingWithOptions:)
方法中設定FirebaseApp
共用例項:SwiftUI
// Use Firebase library to configure APIs FirebaseApp.configure()
Swift
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- 如果您使用 SwiftUI,則必須建立應用程式委派,並透過
UIApplicationDelegateAdaptor
或NSApplicationDelegateAdaptor
將其附加至App
結構體。您也必須停用應用程式委派功能切換功能。詳情請參閱 SwiftUI 操作說明。SwiftUI
@main struct YourApp: App { // register app delegate for Firebase setup @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate var body: some Scene { WindowGroup { NavigationView { ContentView() } } } }
- 呼叫
signInAnonymouslyWithCompletion:
方法:Swift
Auth.auth().signInAnonymously { authResult, error in // ... }
Objective-C
[[FIRAuth auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { // ... }];
- 如果
signInAnonymouslyWithCompletion:
方法執行完畢且沒有發生錯誤,您可以透過FIRAuthDataResult
物件取得匿名使用者的帳戶資料:Swift
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
:Google 登入
Swift
guard let authentication = user?.authentication, let idToken = authentication.idToken else { return } let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: authentication.accessToken)
Objective-C
FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:result.user.idToken.tokenString accessToken:result.user.accessToken.tokenString];
Facebook 登入
Swift
let credential = FacebookAuthProvider .credential(withAccessToken: AccessToken.current!.tokenString)
Objective-C
FIRAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken:[FBSDKAccessToken currentAccessToken].tokenString];
使用電子郵件密碼登入
Swift
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
Objective-C
FIRAuthCredential *credential = [FIREmailAuthProvider credentialWithEmail:email password:password];
將
FIRAuthCredential
物件傳遞至登入使用者的linkWithCredential:completion:
方法:Swift
user.link(with: credential) { authResult, error in // ... } }
Objective-C
[[FIRAuth auth].currentUser linkWithCredential:credential completion:^(FIRAuthDataResult *result, NSError *_Nullable error) { // ... }];
如果呼叫 linkWithCredential:completion:
成功,使用者的新帳戶可以存取匿名帳戶的 Firebase 資料。
自動清理
如果您已將專案升級為透過 Identity Platform 使用 Firebase 驗證,即可在 Firebase 控制台啟用自動清除功能。啟用這項功能即代表您允許 Firebase 自動刪除 30 天前的匿名帳戶。如果專案已啟用自動清理功能,匿名驗證就不會計入用量限製或帳單配額。
- 啟用自動清理功能之後,建立的所有匿名帳戶可能會在建立 30 天後自動刪除。
- 啟用自動清除功能後,現有的匿名帳戶會在 30 天後自動刪除。
- 停用自動清除功能後,所有已排定刪除的匿名帳戶都會維持排定的刪除狀態。
- 如果您以連結任何登入方式「升級」匿名帳戶,系統不會自動刪除該帳戶。
如果您想在啟用這項功能前瞭解會受到影響的使用者人數,並已將專案升級至透過 Identity Platform 進行 Firebase 驗證,可以在 Cloud Logging 中使用 is_anon
進行篩選。
後續步驟
現在使用者可以透過 Firebase 進行驗證,因此您可以使用 Firebase 規則控管他們對 Firebase 資料庫資料的存取權。