在 iOS SDK 的 Observer 模式下展示付费墙编辑工具付费墙
如果你已通过付费墙编辑工具自定义了付费墙,则无需在移动端代码中手动处理渲染逻辑。此类付费墙已包含展示内容及展示方式的完整配置。
本节仅适用于 Observer 模式。如果你不在 Observer 模式下工作,请参阅 iOS - 展示付费墙编辑工具付费墙。
开始展示流程前的准备工作(点击展开)
- 完成 Adapty 与 App Store 的初始集成。
- 安装并配置 Adapty SDK,确保将
observerMode参数设置为true。参阅 iOS SDK 安装指南。 - 在 Adapty 看板中创建产品。
- 在编辑工具中配置流程或付费墙,并将产品分配给它们。
- 创建版位并将流程或付费墙分配到版位。
- 在移动端代码中获取流程及其配置。
-
实现
AdaptyObserverModeResolver对象。该协议与 SDK v3 相同——Observer 模式本身在流程和付费墙渲染之间没有区别:func observerMode(didInitiatePurchase product: AdaptyPaywallProduct, onStartPurchase: @escaping () -> Void, onFinishPurchase: @escaping () -> Void) { // use the product object to handle the purchase // call onStartPurchase / onFinishPurchase to notify AdaptyUI about the purchase progress } func observerModeDidInitiateRestorePurchases(onStartRestore: @escaping () -> Void, onFinishRestore: @escaping () -> Void) { // call onStartRestore / onFinishRestore to notify AdaptyUI about the restore progress } -
创建流程配置对象,将你的 resolver 作为
observerModeResolver:参数传入:do { let flowConfiguration = try await AdaptyUI.getFlowConfiguration( forFlow: flow, observerModeResolver: <AdaptyObserverModeResolver> ) } catch { // handle the error }请求参数:
参数 是否必填 描述 forFlow 必填 通过 Adapty.getFlow(placementId:)获取的AdaptyFlow对象。参阅获取流程和付费墙。observerModeResolver 必填 你在上一步实现的 AdaptyObserverModeResolver。 -
使用
AdaptyUI.flowController(with:delegate:)初始化流程控制器:import AdaptyUI let visualFlow = try AdaptyUI.flowController( with: flowConfiguration, delegate: <AdaptyFlowControllerDelegate> )请求参数:
参数 是否必填 描述 flowConfiguration 必填 包含流程视觉细节的 AdaptyUI.FlowConfiguration对象。参阅获取流程和付费墙。delegate 必填 用于监听流程事件的 AdaptyFlowControllerDelegate。参阅处理流程和付费墙事件。返回值:
对象 描述 AdaptyFlowController 表示所请求流程页面的对象。 -
展示控制器:
present(visualFlow, animated: true)
请务必将付费墙关联到购买交易,否则 Adapty 将无法确认购买来源的付费墙。
在 SwiftUI 中,使用 resolver 获取流程配置,并将其传入 .flow 修饰符:
@State var flowPresented = false
@State var flowConfiguration: AdaptyUI.FlowConfiguration?
var body: some View {
Text("Hello, AdaptyUI!")
.flow(
isPresented: $flowPresented,
flowConfiguration: flowConfiguration,
didPerformAction: { action in
switch action {
case .close:
flowPresented = false
default:
break
}
},
didFailPurchase: { product, error in /* handle the error */ },
didFinishRestore: { profile in /* check access level and dismiss */ },
didFailRestore: { error in /* handle the error */ },
didReceiveError: { error in flowPresented = false }
)
.task {
flowConfiguration = try? await AdaptyUI.getFlowConfiguration(
forFlow: flow,
observerModeResolver: <AdaptyObserverModeResolver>
)
}
}getFlowConfiguration 上的 observerModeResolver: 参数使渲染的流程遵循你的自定义购买逻辑——修饰符本身使用与完整模式相同的回调。
请务必将付费墙关联到购买交易,否则 Adapty 将无法确认购买来源的付费墙。
开始展示付费墙前的准备工作(点击展开)
- 完成 Adapty 与 Google Play 及 App Store 的初始集成。
- 安装并配置 Adapty SDK,确保将
observerMode参数设置为true。参阅适用于 iOS 的框架专属说明。 - 在 Adapty 看板中创建产品。
- 在 Adapty 看板中配置付费墙、将产品分配给付费墙,并使用付费墙编辑工具进行自定义。
- 在 Adapty 看板中创建版位并将付费墙分配到版位。
- 在移动端代码中获取付费墙编辑工具付费墙及其配置。
-
实现
AdaptyObserverModeResolver对象:func observerMode(didInitiatePurchase product: AdaptyPaywallProduct, onStartPurchase: @escaping () -> Void, onFinishPurchase: @escaping () -> Void) { // use the product object to handle the purchase // use the onStartPurchase and onFinishPurchase callbacks to notify AdaptyUI about the process of the purchase } func observerModeDidInitiateRestorePurchases(onStartRestore: @escaping () -> Void, onFinishRestore: @escaping () -> Void) { // use the onStartRestore and onFinishRestore callbacks to notify AdaptyUI about the process of the restore }observerMode(didInitiatePurchase:onStartPurchase:onFinishPurchase:)事件会通知你用户已发起购买,你可以在此回调中触发自定义购买流程。observerModeDidInitiateRestorePurchases(onStartRestore:onFinishRestore:)事件会通知你用户已发起恢复购买,你可以在此回调中触发自定义恢复流程。另外,请记得调用以下回调,以便将购买或恢复的进度通知 AdaptyUI。这对付费墙的正常行为(例如显示加载动画等)是必要的:
回调 描述 onStartPurchase() 调用此回调以通知 AdaptyUI 购买已开始。 onFinishPurchase() 调用此回调以通知 AdaptyUI 购买已完成。 onStartRestore() 调用此回调以通知 AdaptyUI 恢复购买已开始。 onFinishRestore() 调用此回调以通知 AdaptyUI 恢复购买已完成。 -
创建付费墙配置对象:
do { let paywallConfiguration = try AdaptyUI.getPaywallConfiguration( forPaywall: <paywall object>, observerModeResolver: <AdaptyObserverModeResolver> ) } catch { // handle the error }请求参数:
参数 是否必填 描述 Paywall 必填 用于获取目标付费墙控制器的 AdaptyPaywall对象。ObserverModeResolver 必填 你在上一步实现的 AdaptyObserverModeResolver对象。 -
使用
.paywallController(for:products:viewConfiguration:delegate:)方法初始化要展示的可视化付费墙:import AdaptyUI let visualPaywall = AdaptyUI.paywallController( with: <paywall configuration object>, delegate: <AdaptyPaywallControllerDelegate> )
请求参数:
| 参数 | 是否必填 | 描述 |
|---|---|---|
| Paywall Configuration | 必填 | 包含付费墙视觉细节的 AdaptyUI.PaywallConfiguration 对象。使用 AdaptyUI.getPaywallConfiguration(forPaywall:locale:) 方法。详情参阅获取付费墙编辑工具付费墙及其配置。 |
| Delegate | 必填 | 用于监听付费墙事件的 AdaptyPaywallControllerDelegate。详情参阅处理付费墙事件。 |
返回值:
| 对象 | 描述 |
|---|---|
| AdaptyPaywallController | 表示所请求付费墙页面的对象。 |
对象创建成功后,可以这样展示它:
present(visualPaywall, animated: true)请务必将付费墙关联到购买交易,否则 Adapty 将无法确认购买来源的付费墙。
在 SwiftUI 中,使用 .paywall 修饰符在设备屏幕上展示可视化付费墙:
@State var paywallPresented = false
var body: some View {
Text("Hello, AdaptyUI!")
.paywall(
isPresented: $paywallPresented,
paywallConfiguration: <paywall configuration object>,
didPerformAction: { action in
switch action {
case .close:
paywallPresented = false
default:
// Handle other actions
break
}
},
didFinishRestore: { profile in /* check access level and dismiss */ },
didFailRestore: { error in /* handle the error */ },
didFailRendering: { error in paywallPresented = false }
)
}请求参数:
| 参数 | 是否必填 | 描述 |
|---|---|---|
| Paywall Configuration | 必填 | 包含付费墙视觉细节的 AdaptyUI.PaywallConfiguration 对象。使用 AdaptyUI.getPaywallConfiguration(forPaywall:locale:) 方法。详情参阅获取付费墙编辑工具付费墙及其配置。 |
| Products | 可选 | 提供 AdaptyPaywallProduct 对象数组,以优化产品在屏幕上的显示时机。若传入 nil,AdaptyUI 将自动获取所需产品。 |
| TagResolver | 可选 | 定义自定义标签及其解析值的字典。自定义标签作为付费墙内容中的占位符,会被动态替换为特定字符串,以实现个性化内容。详情参阅付费墙编辑工具中的自定义标签相关主题。 |
| ObserverModeResolver | 可选 | 你在上一步实现的 AdaptyObserverModeResolver 对象。 |
闭包参数:
| 闭包参数 | 描述 |
|---|---|
| didFinishRestore | 若 Adapty.restorePurchases() 成功,此回调将被调用。 |
| didFailRestore | 若 Adapty.restorePurchases() 失败,此回调将被调用。 |
| didFailRendering | 若界面渲染过程中发生错误,此回调将被调用。 |
其他闭包参数请参阅 iOS - 处理事件。
请务必将付费墙关联到购买交易,否则 Adapty 将无法确认购买来源的付费墙。
开始展示付费墙前的准备工作(点击展开)
- 完成 Adapty 与 Google Play 及 App Store 的初始集成。
- 安装并配置 Adapty SDK,确保将
observerMode参数设置为true。参阅适用于 iOS、React Native、Flutter 和 Unity 的框架专属说明。 - 在 Adapty 看板中创建产品。
- 在 Adapty 看板中配置付费墙、将产品分配给付费墙,并使用付费墙编辑工具进行自定义。
- 在 Adapty 看板中创建版位并将付费墙分配到版位。
- 在移动端代码中获取付费墙编辑工具付费墙及其配置。
-
实现
AdaptyObserverModeDelegate对象:func paywallController(_ controller: AdaptyPaywallController, didInitiatePurchase product: AdaptyPaywallProduct, onStartPurchase: @escaping () -> Void, onFinishPurchase: @escaping () -> Void) { // use the product object to handle the purchase // use the onStartPurchase and onFinishPurchase callbacks to notify AdaptyUI about the process of the purchase }paywallController(_:didInitiatePurchase:onStartPurchase:onFinishPurchase:)事件会通知你用户已发起购买,你可以在此事件中触发自定义购买流程。另外,请记得调用以下回调,以便将购买进度通知 AdaptyUI。这对付费墙的正常行为(例如显示加载动画等)是必要的:
回调 描述 onStartPurchase 调用此回调以通知 AdaptyUI 购买已开始。 onFinishPurchase 调用此回调以通知 AdaptyUI 购买已完成。 -
使用
.paywallController(for:products:viewConfiguration:delegate:observerModeDelegate:)方法初始化要展示的可视化付费墙:import AdaptyUI let visualPaywall = AdaptyUI.paywallController( for: <paywall object>, products: <paywall products array>, viewConfiguration: <LocalizedViewConfiguration>, delegate: <AdaptyPaywallControllerDelegate> observerModeDelegate: <AdaptyObserverModeDelegate> )
请求参数:
| 参数 | 是否必填 | 描述 |
|---|---|---|
| Paywall | 必填 | 用于获取目标付费墙控制器的 AdaptyPaywall 对象。 |
| Products | 可选 | 提供 AdaptyPaywallProduct 对象数组,以优化产品在屏幕上的显示时机。若传入 nil,AdaptyUI 将自动获取所需产品。 |
| ViewConfiguration | 必填 | 包含付费墙视觉细节的 AdaptyUI.LocalizedViewConfiguration 对象。使用 AdaptyUI.getViewConfiguration(paywall:locale:) 方法。详情参阅获取付费墙编辑工具付费墙及其配置。 |
| Delegate | 必填 | 用于监听付费墙事件的 AdaptyPaywallControllerDelegate。详情参阅处理付费墙事件。 |
| ObserverModeDelegate | 必填 | 你在上一步实现的 AdaptyObserverModeDelegate 对象。 |
| TagResolver | 可选 | 定义自定义标签及其解析值的字典。自定义标签作为付费墙内容中的占位符,会被动态替换为特定字符串,以实现个性化内容。详情参阅付费墙编辑工具中的自定义标签相关主题。 |
返回值:
| 对象 | 描述 |
|---|---|
| AdaptyPaywallController | 表示所请求付费墙页面的对象。 |
对象创建成功后,可以这样展示它:
present(visualPaywall, animated: true)请务必将付费墙关联到购买交易,否则 Adapty 将无法确认购买来源的付费墙。
在 SwiftUI 中,使用 .paywall 修饰符在设备屏幕上展示可视化付费墙:
@State var paywallPresented = false
var body: some View {
Text("Hello, AdaptyUI!")
.paywall(
isPresented: $paywallPresented,
paywall: <paywall object>,
configuration: <LocalizedViewConfiguration>,
didPerformAction: { action in
switch action {
case .close:
paywallPresented = false
default:
// Handle other actions
break
}
},
didFinishRestore: { profile in /* check access level and dismiss */ },
didFailRestore: { error in /* handle the error */ },
didFailRendering: { error in paywallPresented = false },
observerModeDidInitiatePurchase: { product, onStartPurchase, onFinishPurchase in
// use the product object to handle the purchase
// use the onStartPurchase and onFinishPurchase callbacks to notify AdaptyUI about the process of the purchase
},
)
}请求参数:
| 参数 | 是否必填 | 描述 |
|---|---|---|
| Paywall | 必填 | 用于获取目标付费墙控制器的 AdaptyPaywall 对象。 |
| Product | 可选 | 提供 AdaptyPaywallProduct 对象数组,以优化产品在屏幕上的显示时机。若传入 nil,AdaptyUI 将自动获取所需产品。 |
| Configuration | 必填 | 包含付费墙视觉细节的 AdaptyUI.LocalizedViewConfiguration 对象。使用 AdaptyUI.getViewConfiguration(paywall:locale:) 方法。详情参阅获取付费墙编辑工具付费墙及其配置。 |
| TagResolver | 可选 | 定义自定义标签及其解析值的字典。自定义标签作为付费墙内容中的占位符,会被动态替换为特定字符串,以实现个性化内容。详情参阅付费墙编辑工具中的自定义标签相关主题。 |
闭包参数:
| 闭包参数 | 描述 |
|---|---|
| didFinishRestore | 若 Adapty.restorePurchases() 成功,此回调将被调用。 |
| didFailRestore | 若 Adapty.restorePurchases() 失败,此回调将被调用。 |
| didFailRendering | 若界面渲染过程中发生错误,此回调将被调用。 |
| observerModeDidInitiatePurchase | 当用户发起购买时,此回调将被调用。 |
其他闭包参数请参阅 iOS - 处理事件。
请务必将付费墙关联到购买交易,否则 Adapty 将无法确认购买来源的付费墙。