如果你的用户可以在网站上购买产品,你可以让他们的访问等级在移动应用中自动保持同步。
本指南将介绍如何通过 Adapty API 和 SDK 实现这一功能。
使用场景示例
假设在你的应用中,用户可以在移动端和网页端注册免费版计划。你允许他们在网站上通过 Stripe 或 Chargebee 升级到高级计划。
用户在网页端完成订阅后,你希望他们立即在移动应用中获得高级访问权限——无需等待,也无需重新登录。
这正是 Adapty 帮你自动化实现的功能。
第一步:识别用户
Adapty 使用 customer_user_id 来跨平台识别用户。
你需要创建一个 ID,并同时传递给移动端 SDK 和 Web 后端。
从网页注册
当您的用户在网站上注册时,您需要使用服务端 API 在 Adapty 中为他们创建用户画像。
请参阅此处的方法参考文档。
curl --request POST \
--url https://api.adapty.io/api/v2/server-side-api/profile/ \
--header 'Accept: application/json' \
--header 'Authorization: Api-Key YOUR_SECRET_API_KEY' \
--header 'Content-Type: application/json' \
--header 'adapty-customer-user-id: YOUR_CUSTOMER_USER_ID'
从应用内注册
当用户首次从应用内注册时,你可以在 SDK 激活时传入其 customer user ID;如果在注册阶段之前已经激活了 Adapty SDK,则可以使用 identify 方法创建新的用户画像并为其分配 customer user ID。
如果在 SDK 激活之后才识别新用户,SDK 会先创建一个匿名用户画像,因为它必须依赖某个用户画像才能正常运行。随后,当你识别该用户并为其分配新的 customer user ID 时,系统将创建一个新的用户画像。
这是完全正常的行为,不会影响数据分析的准确性。详情请参阅此处。
do {
try await Adapty.identify("YOUR_USER_ID") // Unique for each user
} catch {
// handle the error
}
// User IDs must be unique for each user
Adapty.identify("YOUR_USER_ID") { error in
if let error {
// handle the error
}
}
Adapty.identify("YOUR_USER_ID") { error -> // Unique for each user
if (error == null) {
// successful identify
}
}
// User IDs must be unique for each user
Adapty.identify("YOUR_USER_ID", error -> {
if (error == null) {
// successful identify
}
});
try {
await adapty.identify("YOUR_USER_ID"); // Unique for each user
// successfully identified
} catch (error) {
// handle the error
}
try {
await Adapty().identify(customerUserId); // Unique for each user
} on AdaptyError catch (adaptyError) {
// handle the error
} catch (e) {
}
Adapty.Identify("YOUR_USER_ID", (error) => { // Unique for each user
if(error == null) {
// successful identify
}
});
Adapty.identify("YOUR_USER_ID") // Unique for each user
.onSuccess {
// successful identify
}
.onError { error ->
// handle the error
}
try {
await adapty.identify({ customerUserId: "YOUR_USER_ID" });
// successfully identified
} catch (error) {
// handle the error
}
步骤 2:通过 API 检查订阅状态
当用户在您的网站上登录时,使用 API 获取其 Adapty 用户画像。
如果用户没有活跃的订阅,您可以展示付费墙。
请参阅此处的方法参考文档。
curl --request GET \
--url https://api.adapty.io/api/v2/server-side-api/profile/ \
--header 'Accept: application/json' \
--header 'Authorization: Api-Key YOUR_SECRET_API_KEY' \
--header 'adapty-customer-user-id: YOUR_USER_ID' \
步骤 3:在您的网站上展示付费墙
在您的网站上,为免费增值用户展示付费墙。
您可以使用任何支付服务商(Stripe、Chargebee、LemonSqueezy 等)。
第四步:在 Adapty 中更新订阅状态
用户在您的网站完成支付后,调用 Adapty API 根据所购产品更新该用户的访问等级。
方法参考文档请见这里。
curl --request POST \
--url https://api.adapty.io/api/v2/server-side-api/purchase/profile/grant/access-level/ \
--header 'Accept: application/json' \
--header 'Authorization: Api-Key YOUR_SECRET_API_KEY' \
--header 'Content-Type: application/json' \
--header 'adapty-customer-user-id: YOUR_USER_ID' \
--data '{
"access_level_id": "YOUR_ACCESS_LEVEL"
}'
第五步:在应用中同步状态
当用户打开你的移动应用时,拉取最新的用户画像并解锁付费功能。
你需要获取用户画像,或让其自动同步。然后从中读取访问等级。
下面展示了如何获取用户画像并检查其状态。更多详情请参阅这里。
do {
let profile = try await Adapty.getProfile()
if profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive ?? false {
// grant access to premium features
}
} catch {
// handle the error
}
Adapty.getProfile { result in
if let profile = try? result.get() {
// check the access
if profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive ?? false {
// grant access to premium features
}
}
}
Adapty.getProfile { result ->
when (result) {
is AdaptyResult.Success -> {
val profile = result.value
// check the access
if (profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive == true) {
// grant access to premium features
}
}
is AdaptyResult.Error -> {
val error = result.error
// handle the error
}
}
}
Adapty.getProfile(result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyProfile profile = ((AdaptyResult.Success<AdaptyProfile>) result).getValue();
// check the access
if (profile.getAccessLevels().get("YOUR_ACCESS_LEVEL") != null && profile.getAccessLevels().get("YOUR_ACCESS_LEVEL").getIsActive()) {
// grant access to premium features
}
} else if (result instanceof AdaptyResult.Error) {
AdaptyError error = ((AdaptyResult.Error) result).getError();
// handle the error
}
});
try {
const profile = await adapty.getProfile();
// check the access
if (profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive) {
// grant access to premium features
}
} catch (error) {
// handle the error
}
try {
final profile = await Adapty().getProfile();
// check the access
if (profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive ?? false) {
// grant access to premium features
}
} on AdaptyError catch (adaptyError) {
// handle the error
} catch (e) {
}
Adapty.GetProfile((profile, error) => {
if (error != null) {
// handle the error
return;
}
// check the access
if (profile.AccessLevels["YOUR_ACCESS_LEVEL"]?.IsActive ?? false) {
// grant access to premium features
}
});
Adapty.getProfile()
.onSuccess { profile ->
// check the access
if (profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive == true) {
// grant access to premium features
}
}
.onError { error ->
// handle the error
}
try {
const profile = await adapty.getProfile();
// 检查访问权限
if (profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive) {
// 授予高级功能访问权限
}
} catch (error) {
// 处理错误
}