为 Kotlin 多平台 实现应用内购买
使用 Adapty 的 Kotlin 多平台 (KMP) SDK 在 iOS 和 Android 上集成应用内购买。处理收据,跟踪订阅状态,免去构建后端的需要。所有的功能都在一次集成中。
Adapty.activate(AdaptyConfig.Builder("YOUR_PUBLIC_SDK_KEY").build())
// Make a purchase, Adapty handles the rest
Adapty.makePurchase(product)
.onSuccess { purchaseResult ->
if (purchaseResult is AdaptyPurchaseResult.Success)
// successful purchase
}
.onError { error ->
// handle the error
}
你无需为 Kotlin 多平台 应用内购买功能构建后端
Adapty 的 SDK 处理通常在服务器上执行的所有事务:收据验证、订阅状态、续订、试用和退款。您只需连接一次,即可获得一个工作 Kotlin 多平台应用内购买设置。
安排演示为什么选择 Adapty SDK?
跨平台追踪订阅状态
你将始终知道用户在 iOS 和 Android 上是否拥有活跃订阅。
在后端验证收据
无需自建验证系统,Adapty 会自动完成。
处理各种订阅状态
支持免费试用、升级、促销优惠、家庭共享、续订等。
企业级可扩展核心
我们频繁更新,保持 SDK 稳定,并运行在 >99.99% SLA 上。
配置平台
安装 Adapty SDK
await adapty.activate( 'PUBLIC_SDK_KEY', { customerUserId: 'YOUR_USER_ID', });
处理购买事件
Kotlin 多平台 的简单集成
只需几个小时即可完成 Adapty SDK 设置,并在 iOS 和 Android 上开始使用应用内购买功能。需要帮助?查看我们的完整 Kotlin 多平台 IAP 教程。
使用 5 个 SDK 方法处理 Kotlin 多平台 IAP
import com.adapty.kmp.Adapty
import com.adapty.kmp.models.AdaptyPurchaseResult
Adapty.makePurchase(product = product).onSuccess { purchaseResult ->
when (purchaseResult) {
is AdaptyPurchaseResult.Success -> {
val profile = purchaseResult.profile
if (profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive == true) {
// Grant access to the paid features
}
}
is AdaptyPurchaseResult.UserCanceled -> {
// Handle the case where the user canceled the purchase
}
is AdaptyPurchaseResult.Pending -> {
// Handle deferred purchases (e.g., the user will pay offline with cash)
}
}
}.onError { error ->
// Handle the error
}import com.adapty.kmp.Adapty
Adapty.restorePurchases().onSuccess { profile ->
if (profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive == true) {
// successful access restore
}
}.onError { error ->
// handle the error
}
import com.adapty.kmp.Adapty
Adapty.identify("YOUR_USER_ID") // Unique for each user
.onSuccess {
// successful identify
}
.onError { error ->
// handle the error
}
import com.adapty.kmp.Adapty
import com.adapty.kmp.models.AdaptyProfile
import com.adapty.kmp.models.AdaptyProfileParameters
val builder = AdaptyProfileParameters.Builder()
.withEmail("[email protected]")
.withPhoneNumber("+18888888888")
.withFirstName("John")
.withLastName("Appleseed")
.withGender(AdaptyProfile.Gender.FEMALE)
.withBirthday(AdaptyProfile.Date(1970, 1, 3))
Adapty.updateProfile(builder.build())
.onSuccess {
// profile updated successfully
}
.onError { error ->
// handle the error
}
import com.adapty.kmp.models.AdaptyProfileParameters
val builder = AdaptyProfileParameters.Builder()
// You can set your own custom attributes:
builder.withCustomAttribute("key1", "value1")
// To remove existing key, use .withRemovedCustomAttribute() method:
builder.withRemovedCustomAttribute("key2")











