在 Kotlin Multiplatform SDK 中检查订阅状态

要判断用户是否可以访问付费内容或查看付费墙,你需要检查用户画像中的访问等级。

本文介绍如何访问用户画像状态,以决定用户应看到什么内容——是显示付费墙还是授予付费功能的访问权限。

获取订阅状态

当您决定是否向用户显示付费墙或付费内容时,需要检查用户画像中的访问等级。您有两种选择:

  • 如果需要立即获取最新的用户画像数据(例如应用启动时)或希望强制更新,请调用 getProfile。
  • 设置自动用户画像更新,以保留一份本地副本,该副本会在订阅状态发生变化时自动刷新。

获取用户画像

获取订阅状态最简单的方式是使用 getProfile 方法来访问用户画像:

Adapty.getProfile()
    .onSuccess { profile ->
        // check the access
    }
    .onError { error ->
        // handle the error
    }

监听订阅更新

要在应用中自动接收用户画像更新:

  1. 使用 Adapty.setOnProfileUpdatedListener() 监听用户画像变更——每当用户的订阅状态发生变化时,Adapty 会自动调用此方法。
  2. 在该方法被调用时保存更新后的用户画像数据,以便在整个应用中直接使用,无需发起额外的网络请求。
class SubscriptionManager {
    private var currentProfile: AdaptyProfile? = null
    
    init {
        // Listen for profile updates
        Adapty.setOnProfileUpdatedListener { profile ->
            currentProfile = profile
            // Update UI, unlock content, etc.
        }
    }
    
    // Use stored profile instead of calling getProfile()
    fun hasAccess(): Boolean {
        return currentProfile?.accessLevels?.get("YOUR_ACCESS_LEVEL")?.isActive == true
    }
}
Note

Adapty 会在应用启动时自动调用用户画像更新监听器,即使设备处于离线状态,也能提供缓存的订阅数据。

将用户画像与付费墙逻辑关联

当你需要立即决定是否显示付费墙或开放付费功能时,可以直接检查用户画像。这种方式适用于应用启动、进入高级功能区域或显示特定内容之前等场景。

private fun checkAccessAndShowPaywall() {
    // First, check if user has access
    Adapty.getProfile()
        .onSuccess { profile ->
            val hasAccess = profile.accessLevels?.get("YOUR_ACCESS_LEVEL")?.isActive == true
            
            if (!hasAccess) {
                // User doesn't have access, show paywall
                showPaywall()
            } else {
                // User has access, show premium content
                showPremiumContent()
            }
        }
        .onError { error ->
            // If we can't check access, show paywall as fallback
            showPaywall()
        }
}

private fun showPaywall() {
    // Get and display the flow using the KMP SDK
    Adapty.getFlow("YOUR_PLACEMENT_ID")
        .onSuccess { flow ->
            if (flow.hasViewConfiguration) {
                AdaptyUI.createFlowView(flow = flow)
                    .onSuccess { view ->
                        view.present()
                    }
                    .onError { error ->
                        showError("Unable to display the paywall")
                    }
            } else {
                // Handle remote config paywall or show custom UI
                handleRemoteConfigPaywall(flow)
            }
        }
        .onError { error ->
            // Handle flow loading error
            showError("Unable to load paywall")
        }
}

private fun showPremiumContent() {
    // Show your premium content here
    // This is where you unlock paid features
}

后续步骤

现在,当您了解如何追踪订阅状态后,请学习如何使用用户画像,以确保用户能够访问其已付费的内容。