在 iOS SDK 中以观察者模式展示流程
安装 AI Agent 工具
在你的 AI 编程工具中完成整个 Adapty SDK 集成。 了解更多
可安装到任何支持技能的 AI 工具。之后更新请运行 npx skills update。
这些技能涵盖 SDK 集成、构建 flow 和 paywall、发布前审查、placement 迁移等。用日常语言描述任务,你的 AI Agent 会自动选择合适的技能。
如果你使用观察者模式,则需要自行处理购买逻辑。但你可以将付费墙 UI 交由 Flow & Paywall Builder 来管理。
在编辑工具中设计屏幕,Adapty SDK 将其原生渲染。随时更新文案、布局和定价,无需发布新版本。当用户点击购买按钮时,SDK 会调用你的代码来完成购买。
本指南介绍如何展示该屏幕并处理购买回调。如果由 Adapty 为你完成购买,请参阅展示流程与付费墙。
开始之前
你需要:
- Adapty iOS SDK v4 或更高版本:旧版本无法渲染流程。若要从 v3 迁移现有应用,请参阅迁移指南。
observerMode设置为true:请参阅 iOS SDK 安装指南。- 初始商店集成:通过 App Store 配置 Adapty。
- 已发布的包含产品的流程:在流程与付费墙编辑工具中构建,并为其分配产品。
- 流程版位:创建版位并将您的流程分配至该版位。
- 在应用中获取流程:在展示流程之前,获取流程及其配置。
实现
-
实现
AdaptyObserverModeResolver对象:func observerMode(didInitiatePurchase product: AdaptyPaywallProduct, onStartPurchase: @MainActor @Sendable @escaping () -> Void, onFinishPurchase: @MainActor @Sendable @escaping () -> Void) { // use the product object to handle the purchase // call onStartPurchase / onFinishPurchase to notify AdaptyUI about the purchase progress } func observerModeDidInitiateRestorePurchases(onStartRestore: @MainActor @Sendable @escaping () -> Void, onFinishRestore: @MainActor @Sendable @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 | required | 通过 Adapty.getFlow(placementId:) 获取的 AdaptyFlow 对象。请参阅获取流程与付费墙。 |
| observerModeResolver | required | 您在上面实现的 AdaptyObserverModeResolver。 |
-
使用
AdaptyUI.flowController(with:delegate:)初始化流程控制器:import AdaptyUI let visualFlow = try AdaptyUI.flowController( with: flowConfiguration, delegate: <AdaptyFlowControllerDelegate> )请求参数:
| 参数 | 是否必需 | 描述 |
|---|---|---|
| flowConfiguration | required | 包含流程视觉细节的 AdaptyUI.FlowConfiguration 对象。请参阅获取流程与付费墙。 |
| delegate | required | 用于监听流程事件的 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
}
},
didFinishPurchase: { product, purchaseResult in /* dismiss, or do nothing to let the flow continue */ },
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 的初始集成。
2. 安装并配置 Adapty SDK。请确保将
observerMode参数设置为true。请参阅针对 iOS 的框架专属说明。 3. 在 Adapty 看板中创建产品。 4. 在 Adapty 看板中配置付费墙并为其分配产品,并使用付费墙编辑工具进行自定义。 5. 在 Adapty 看板中创建版位并为其分配付费墙。 6. 在移动应用代码中获取付费墙编辑工具的付费墙及其配置。
-
实现
AdaptyObserverModeResolver对象:func observerMode(didInitiatePurchase product: AdaptyPaywallProduct, onStartPurchase: @MainActor @Sendable @escaping () -> Void, onFinishPurchase: @MainActor @Sendable @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: @MainActor @Sendable @escaping () -> Void, onFinishRestore: @MainActor @Sendable @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 | required | 用于获取所需付费墙控制器的 AdaptyPaywall 对象。 |
| ObserverModeResolver | required | 您在上一步中实现的 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 }
)
}Request parameters:
| 参数 | 是否必填 | 描述 |
|---|---|---|
| Paywall Configuration | 必填 | 包含付费墙视觉详情的 AdaptyUI.PaywallConfiguration 对象。使用 AdaptyUI.getPaywallConfiguration(forPaywall:locale:) 方法。详情请参阅获取付费墙编辑工具付费墙及其配置。 |
| Products | 可选 | 提供 AdaptyPaywallProduct 对象数组,以优化产品在屏幕上的展示时机。如果传入 nil,AdaptyUI 将自动获取所需产品。 |
| TagResolver | 可选 | 定义自定义标签及其解析值的字典。自定义标签在付费墙内容中充当占位符,动态替换为特定字符串,以实现付费墙内的个性化内容。详情请参阅付费墙编辑工具中的自定义标签主题。 |
| ObserverModeResolver | 可选 | 您在上一步中实现的 AdaptyObserverModeResolver 对象。 |
闭包参数:
| 闭包参数 | 描述 |
|---|---|
| didFinishRestore | 如果 Adapty.restorePurchases() 执行成功,将调用此回调。 |
| didFailRestore | 如果 Adapty.restorePurchases() 执行失败,将调用此回调。 |
| didFailRendering | 如果在界面渲染过程中发生错误,将调用此回调。 |
请参阅 iOS - 处理事件 主题了解其他 closure 参数。
请不要忘记将付费墙关联到购买交易。否则,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
},
)
}Request parameters:
| 参数 | 是否必填 | 描述 |
|---|---|---|
| Paywall | 必填 | 用于获取目标付费墙控制器的 AdaptyPaywall 对象。 |
| Product | 可选 | 提供 AdaptyPaywallProduct 对象数组,以优化产品在屏幕上的显示时机。如果传入 nil,AdaptyUI 将自动获取所需产品。 |
| Configuration | 必填 | 包含付费墙视觉详情的 AdaptyUI.LocalizedViewConfiguration 对象。使用 AdaptyUI.getViewConfiguration(paywall:locale:) 方法。详情请参阅获取付费墙编辑工具付费墙及其配置主题。 |
| TagResolver | 可选 | 定义自定义标签及其解析值的字典。自定义标签作为付费墙内容中的占位符,在付费墙中动态替换为特定字符串,以实现个性化内容。详情请参阅付费墙编辑工具中的自定义标签主题。 |
Closure parameters:
| 回调参数 | 描述 |
|---|---|
| didFinishRestore | 如果 Adapty.restorePurchases() 执行成功,该回调将被触发。 |
| didFailRestore | 如果 Adapty.restorePurchases() 执行失败,该回调将被触发。 |
| didFailRendering | 如果界面渲染过程中发生错误,该回调将被触发。 |
| observerModeDidInitiatePurchase | 当用户发起购买时,该回调将被触发。 |
有关其他闭包参数,请参阅 iOS - 处理事件 主题。
不要忘记将付费墙与购买交易关联。否则,Adapty 将无法确定购买来源的付费墙。