链接用户身份 — Swift SDK
Atlas Device SDK 已弃用。 有关详细信息,请参阅弃用页面。
Overview
Atlas App Services提供许多身份验证提供程序,用于将用户日志到您的应用。 每个提供商都会创建一个唯一的用户身份。 App Services和Realm Swift SDK允许您将多个档案合并为一个用户凭证。
例子
考虑一个提供匿名登录的应用程序。 这样,用户无需注册即可探索该应用。 如果用户喜欢该应用程序,他们就会创建永久帐户。 他们使用 SSO 或电子邮件/密码身份验证进行注册。 默认情况下,这会创建一个新的 User
对象。 应用程序必须将新身份与原始用户关联。
您可以使用linkUser(credentials:Credentials)
关联身份。 这会将身份验证提供者链接到已登录的User
对象。
@interface LinkIdentitiesExample : NSObject @end @implementation LinkIdentitiesExample { RLMApp *app; RLMUser *anonymousUser; } // Entry-point for example. - (void)runExample { app = [RLMApp appWithId:YOUR_APP_ID]; [self logInAnonymously]; } - (void)logInAnonymously { [app loginWithCredential:[RLMCredentials anonymousCredentials] completion:^(RLMUser *user, NSError *error) { if (error != nil) { NSLog(@"Failed to log in: %@", [error localizedDescription]); return; } // User uses app, then later registers an account [self registerNewAccountAsAnonymousUser: user]; }]; } - (void)registerNewAccountAsAnonymousUser:(RLMUser *)user { NSString *email = @"link2@example.com"; NSString *password = @"ganondorf"; [[app emailPasswordAuth] registerUserWithEmail:email password:password completion:^(NSError *error) { if (error != nil) { NSLog(@"Failed to register new account: %@", [error localizedDescription]); return; } // Successfully created account, now link it // with the existing anon user [self linkUser:self->anonymousUser withCredentials:[RLMCredentials credentialsWithEmail:email password:password]]; }]; } - (void)linkUser:(RLMUser *)user withCredentials:(RLMCredentials *)credentials { [[app currentUser] linkUserWithCredentials:credentials completion:^(RLMUser *user, NSError *error) { if (error != nil) { NSLog(@"Failed to link user: %@", [error localizedDescription]); return; } NSLog(@"Successfully linked user: %@", user); }]; } @end
let app = App(id: YOUR_APP_SERVICES_APP_ID) func logInAnonymously() { app.login(credentials: Credentials.anonymous) { (result) in switch result { case .failure(let error): print("Failed to log in: \(error.localizedDescription)") case .success(let user): // User uses app, then later registers an account registerNewAccount(anonymousUser: user) } } } func registerNewAccount(anonymousUser: User) { let email = "swift-link@example.com" let password = "ganondorf" app.emailPasswordAuth.registerUser(email: email, password: password) { (error) in guard error == nil else { print("Failed to register new account: \(error!.localizedDescription)") return } // Successfully created account, now link it // with the existing anon user link(user: anonymousUser, with: Credentials.emailPassword(email: email, password: password)) } } func link(user: User, with credentials: Credentials) { user.linkUser(credentials: credentials) { (result) in switch result { case .failure(let error): print("Failed to link user: \(error.localizedDescription)") case .success(let user): print("Successfully linked user: \(user)") } } } logInAnonymously()
异步/等待示例
版本 10.16.0 中的新增内容。
Realm Swift SDK 提供了User.linkUser 的异步/等待版本。
let app = App(id: YOUR_APP_SERVICES_APP_ID) func logInAnonymously() async throws -> User { let anonymousUser = try await app.login(credentials: Credentials.anonymous) // User uses app, then later registers an account let newAccountLinkedUser = try await registerNewAccount(anonymousUser: anonymousUser) return newAccountLinkedUser } func registerNewAccount(anonymousUser: User) async throws -> User { let email = "swift-async-link@example.com" let password = "ganondorf" try await app.emailPasswordAuth.registerUser(email: email, password: password) // Successfully created account, now link it // with the existing anon user let linkedUser = try await link(user: anonymousUser, with: Credentials.emailPassword(email: email, password: password)) return linkedUser } func link(user: User, with credentials: Credentials) async throws -> User { try await user.linkUser(credentials: credentials) } do { let linkedUser = try await logInAnonymously() print("Successfully linked user async: \(linkedUser)") } catch { print("Failed to link user: \(error.localizedDescription)") }
从 Realm Swift SDK 10.15.0 和 10.16.0 版本开始,很多 Realm API 均支持 Swift“异步/等待”语法。项目必须符合以下要求:
Swift SDK 版本 | Swift 版本要求 | 支持的操作系统 |
---|---|---|
10.25.0 | Swift 5.6 | iOS 13.x |
10.15.0 或 10.16.0 | Swift 5.5 | iOS 15.x |
如果您的应用会在 async/await
上下文中访问 Realm,请使用 @MainActor
来标记此代码,从而避免出现与线程相关的崩溃。