Adapty 会为每位用户创建一个内部用户画像 ID。但如果您有自己的身份验证系统,则应设置您自己的 Customer User ID。您可以在 用户画像 部分通过 Customer User ID 查找用户,并在服务端 API 中使用它,该 ID 将发送至所有集成。
在配置时设置 Customer User ID
如果您在配置时已有用户 ID,只需将其作为 customerUserId 参数传递给 .activate() 方法:
// In your AppDelegate class:
import Adapty
let configurationBuilder =
AdaptyConfiguration
.builder(withAPIKey: "PUBLIC_SDK_KEY")
.with(customerUserId: "YOUR_USER_ID")
do {
try await Adapty.activate(with: configurationBuilder.build())
} catch {
// handle the error
}
// In your AppDelegate class:
import Adapty
let configurationBuilder =
AdaptyConfiguration
.builder(withAPIKey: "PUBLIC_SDK_KEY")
.with(customerUserId: "YOUR_USER_ID")
Adapty.activate(with: configurationBuilder.build()) { error in
// handle the error
}
想了解 Adapty SDK 如何集成到移动应用中的真实示例?请查看我们的示例应用,其中展示了完整的配置过程,包括显示付费墙、完成购买以及其他基本功能。
在配置后设置 Customer User ID
如果您在 SDK 配置时没有用户 ID,可以随时通过 .identify() 方法在之后设置。最常见的使用场景是在注册或授权之后,即用户从匿名用户切换为已认证用户时。
do {
try await Adapty.identify("YOUR_USER_ID")
} catch {
// handle the error
}
Adapty.identify("YOUR_USER_ID") { error in
if let error {
// handle the error
}
}
请求参数:
- Customer User ID(必填):字符串类型的用户标识符。
重新提交重要用户数据
在某些情况下,例如用户再次登录其账户时,Adapty 的服务器可能已经拥有该用户的信息。在这种情况下,Adapty SDK 将自动切换到使用新用户。如果您向匿名用户传递了任何数据,例如自定义属性或来自第三方网络的归因数据,则应为已识别的用户重新提交这些数据。
同样重要的是,在识别用户后,您应该重新请求所有付费墙和产品,因为新用户的数据可能有所不同。
登出与登录
您可以随时调用 .logout() 方法使用户登出:
do {
try await Adapty.logout()
} catch {
// handle the error
}
Adapty.logout { error in
if error == nil {
// successful logout
}
}
之后,您可以使用 .identify() 方法让用户登录。
设置 appAccountToken
appAccountToken 是一个 UUID,帮助 Apple 的 StoreKit 2 在不同应用安装和设备之间识别用户。
从 Adapty iOS SDK 3.10.2 开始,您可以在配置 SDK 时或识别用户时传递 appAccountToken:
// During configuration:
let configurationBuilder =
AdaptyConfiguration
.builder(withAPIKey: "PUBLIC_SDK_KEY")
.with(customerUserId: "YOUR_USER_ID", withAppAccountToken: UUID())
do {
try await Adapty.activate(with: configurationBuilder.build())
} catch {
// handle the error
}
// Or when identifying a user:
do {
try await Adapty.identify("YOUR_USER_ID", withAppAccountToken: UUID())
} catch {
// handle the error
}
// During configuration:
let configurationBuilder =
AdaptyConfiguration
.builder(withAPIKey: "PUBLIC_SDK_KEY")
.with(customerUserId: "YOUR_USER_ID", withAppAccountToken: UUID())
Adapty.activate(with: configurationBuilder.build()) { error in
// handle the error
}
// Or when identifying a user:
Adapty.identify("YOUR_USER_ID", withAppAccountToken: UUID()) { error in
if let error {
// handle the error
}
}
之后,您可以使用 .identify() 方法让用户登录。