Docs 菜单

Docs 主页开发应用程序Atlas Device SDKs

创建和删除用户 - Swift SDK

在此页面上

  • 创建一个用户
  • 删除用户
  • 使用异步/等待删除用户
  • 使用完成处理程序删除用户

对于大多数身份验证方法,Atlas App Services 会在用户首次进行身份验证时自动创建用户对象。唯一的例外是电子邮件/密码身份验证。使用电子邮件/密码身份验证时,必须先注册确认用户,然后用户才能对 App Services App 进行身份验证。

提示

苹果帐号删除要求

Apple 要求通过其 App Store 列出的应用程序 必须为创建帐户的任何用户提供删除帐户的选项。无论您使用的是必须手动注册用户的身份验证方法(例如电子邮件/密码身份验证),还是自动创建用户的身份验证方法(例如“通过 Apple 登录”),都必须在 6 月 之前实现 用户帐户删除 , 。302022

版本 10.23.0 中的新增功能

您可以调用用户对象的delete方法,将该用户对象从您的应用中删除。除了清除本地数据外,此操作还会从服务器中删除该对象。

重要

删除用户只会删除用户对象,该对象可能包含关联的 元数据 。此操作不会删除应用程序中的 自定义用户数据 或用户输入的数据。 Apple 要求您披露数据保留和删除政策 您的应用程序客户,并为他们提供请求删除用户数据的方法。如果您收集其他用户数据,则必须实施自己的方法或进程来删除这些数据。

如果您的应用程序使用 Apple 的异步/等待语法:

func testAsyncDeleteUser() async throws {
// Logging in using anonymous authentication creates a user object
let syncUser = try await app.login(credentials: Credentials.anonymous)
// Now we have a user, and the total users in the app = 1
XCTAssertNotNil(syncUser)
XCTAssertEqual(app.allUsers.count, 1)
// Call the `delete` method to delete the user
try await syncUser.delete()
// When you delete the user, the SyncSession is destroyed and
// there is no current user.
XCTAssertNil(app.currentUser)
// Now that we've deleted the user, the app has no users.
XCTAssertEqual(app.allUsers.count, 0)
}

从 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 来标记此代码,从而避免出现与线程相关的崩溃。

如果您的应用程序不使用异步/等待:

// Logging in using anonymous authentication creates a user object
app.login(credentials: Credentials.anonymous) { [self] (result) in
switch result {
case .failure(let error):
fatalError("Login failed: \(error.localizedDescription)")
case .success(let user):
// Assign the user object to a variable to demonstrate user deletion
syncUser = user
}
}
// Later, after the user is loggedd in we have a user,
// and the total users in the app = 1
XCTAssertNotNil(syncUser)
XCTAssertEqual(app.allUsers.count, 1)
// Call the `delete` method to delete the user
syncUser!.delete { (error) in
XCTAssertNil(error)
}
// When you delete the user, the SyncSession is destroyed and
// there is no current user.
XCTAssertNil(app.currentUser)
// Now that we've deleted the user, the app has no users.
XCTAssertEqual(app.allUsers.count, 0)
← 管理用户 - Swift SDK