将 AdaptyObserverModeResolver 传入 getFlowConfiguration(forFlow:observerModeResolver:)。Adapty 会在用户点击购买时调用该解析器,而不是自行发起购买。
final class MyObserverModeResolver: AdaptyObserverModeResolver {
func observerMode(
didInitiatePurchase product: AdaptyPaywallProduct,
onStartPurchase: @MainActor @Sendable @escaping () -> Void,
onFinishPurchase: @MainActor @Sendable @escaping () -> Void
) {
Task { @MainActor in
onStartPurchase() // the flow shows its loading indicator
do {
// your own purchase code, returning StoreKit's VerificationResult<Transaction>
let verificationResult = try await myPurchaseApi.purchase(product.vendorProductId)
try await Adapty.reportTransaction(verificationResult, withVariationId: product.variationId)
} catch {
// handle the error
}
onFinishPurchase() // the flow hides the loading indicator
}
}
func observerModeDidInitiateRestorePurchases(
onStartRestore: @MainActor @Sendable @escaping () -> Void,
onFinishRestore: @MainActor @Sendable @escaping () -> Void
) {
Task { @MainActor in
onStartRestore()
try? await myPurchaseApi.restore()
onFinishRestore()
}
}
}
然后展示附加了解析器的流程:
do {
let flowConfiguration = try await AdaptyUI.getFlowConfiguration(
forFlow: flow,
observerModeResolver: MyObserverModeResolver()
)
let visualFlow = try AdaptyUI.flowController(
with: flowConfiguration,
delegate: self // an AdaptyFlowControllerDelegate
)
present(visualFlow, animated: true)
} catch {
// handle the error
}
请参阅在 Observer 模式下展示流程和在 Observer 模式下上报交易。
创建流程视图时传入 AdaptyUiObserverModeHandler——该处理器是 AdaptyUI.getFlowView、AdaptyFlowScreen 和 AdaptyFlowView 的参数。其 onPurchaseInitiated 方法接收购买点击事件。getRestoreHandler 为可选参数——如不传入,恢复购买功能将由 SDK 自行处理。
Warning
请务必传入处理程序。若不传,SDK 会自行完成购买——用户通过 Adapty 完成扣款,而仅有一条日志警告提示此情况。在 iOS 上,缺少处理程序会直接导致购买失败。
val observerModeHandler = object : AdaptyUiObserverModeHandler {
override fun onPurchaseInitiated(
product: AdaptyPaywallProduct,
onStartPurchase: AdaptyUiObserverModeHandler.PurchaseStartCallback,
onFinishPurchase: AdaptyUiObserverModeHandler.PurchaseFinishCallback,
) {
onStartPurchase() // the flow shows its loading indicator
yourBillingClient.makePurchase(
product,
onSuccess = { purchase ->
Adapty.reportTransaction(
TransactionInfo.fromPurchase(purchase),
product.variationId,
) {
onFinishPurchase() // the flow hides the loading indicator
}
},
onError = { onFinishPurchase() },
onCancel = { onFinishPurchase() },
)
}
override fun getRestoreHandler() =
AdaptyUiObserverModeHandler.RestoreHandler { onStartRestore, onFinishRestore ->
onStartRestore()
yourBillingClient.restorePurchases(
onSuccess = { onFinishRestore() },
onError = { onFinishRestore() },
)
}
}
然后将处理器传递给流程视图:
val flowView = AdaptyUI.getFlowView(
activity,
flowConfiguration,
products,
eventListener,
observerModeHandler = observerModeHandler,
)
请参阅在观察者模式下展示流程和在观察者模式下上报交易。
在 AdaptyFlowView 上通过 props 传入 onObserverPurchaseInitiated 和 onObserverRestoreInitiated,或通过 view.setEventHandlers 注册它们。
function MyFlow({ flow }) {
const onObserverPurchaseInitiated = useCallback<
FlowEventHandlers['onObserverPurchaseInitiated']
>((product, onStartPurchase, onFinishPurchase) => {
onStartPurchase(); // the flow shows its loading indicator
myPurchaseApi(product.vendorProductId)
.then((transactionId) => adapty.reportTransaction(transactionId, product.variationId))
.finally(() => onFinishPurchase()); // the flow hides the loading indicator
}, []);
const onObserverRestoreInitiated = useCallback<
FlowEventHandlers['onObserverRestoreInitiated']
>((onStartRestore, onFinishRestore) => {
onStartRestore();
myRestoreApi().finally(() => onFinishRestore());
}, []);
return (
<AdaptyFlowView
flow={flow}
onObserverPurchaseInitiated={onObserverPurchaseInitiated}
onObserverRestoreInitiated={onObserverRestoreInitiated}
// ... your other event handlers
/>
);
}
在嵌入式组件中,处理程序的返回值不会关闭流程——请在购买或恢复成功后自行卸载该组件。
请参阅在 Observer 模式下展示流程和在 Observer 模式下上报交易。
使用 AdaptyUI().setObserverModeResolver 注册解析器——注意是在 AdaptyUI() 上,而非 Adapty 上。需同时实现 observerModeDidInitiatePurchase 和 observerModeDidInitiateRestore;两者均为必填项,且都以流程视图作为第一个参数。
class MyObserverModeResolver extends AdaptyUIObserverModeResolver {
@override
void observerModeDidInitiatePurchase(
AdaptyUIFlowView view,
AdaptyPaywallProduct product,
void Function() onStartPurchase,
void Function() onFinishPurchase,
) async {
onStartPurchase(); // the flow shows its loading indicator
try {
final transactionId = await myPurchaseApi(product.vendorProductId);
await Adapty().reportTransaction(
transactionId: transactionId,
variationId: product.paywallVariationId,
);
} on AdaptyError catch (adaptyError) {
// handle the error
} finally {
onFinishPurchase(); // the flow hides the loading indicator
}
}
@override
void observerModeDidInitiateRestore(
AdaptyUIFlowView view,
void Function() onStartRestore,
void Function() onFinishRestore,
) async {
onStartRestore();
try {
await myRestoreApi();
} finally {
onFinishRestore();
}
}
}
在展示任何流程之前,注册一次解析器:
AdaptyUI().setObserverModeResolver(MyObserverModeResolver());
参见在观察者模式下展示流程和在观察者模式下上报交易。
使用 Adapty.SetObserverModeResolver 注册解析器——注意是在 Adapty 上,而不是 AdaptyUI 上。实现 IAdaptyUIObserverModeResolver 接口,并提供 FlowViewDidInitiatePurchase 和 FlowViewDidInitiateRestore 回调;Unity 的回调名称与其他平台不同。
using System;
using AdaptySDK;
public class MyObserverModeResolver : IAdaptyUIObserverModeResolver
{
public void FlowViewDidInitiatePurchase(
AdaptyUIFlowView view,
AdaptyPaywallProduct product,
Action onStartPurchase,
Action onFinishPurchase
)
{
onStartPurchase(); // the flow shows its loading indicator
MyPurchaseApi.Purchase(product.VendorProductId, (transactionId, purchaseError) =>
{
if (purchaseError != null)
{
onFinishPurchase();
return;
}
Adapty.ReportTransaction(transactionId, product.PaywallVariationId, (reportError) =>
{
onFinishPurchase(); // the flow hides the loading indicator
});
});
}
public void FlowViewDidInitiateRestore(
AdaptyUIFlowView view,
Action onStartRestore,
Action onFinishRestore
)
{
onStartRestore();
MyPurchaseApi.Restore((restoreError) => onFinishRestore());
}
}
在展示任何流程之前,先注册一次解析器:
Adapty.SetObserverModeResolver(new MyObserverModeResolver());
参见在观察者模式下展示流程和在观察者模式下报告交易。
使用 AdaptyUI.setObserverModeResolver 注册解析器。实现 observerModeDidInitiatePurchase 和 observerModeDidInitiateRestore 这两个方法;二者均为必须实现,且都将流程视图作为第一个参数传入。
class MyObserverModeResolver(
private val scope: CoroutineScope,
) : AdaptyUIObserverModeResolver {
override fun observerModeDidInitiatePurchase(
view: AdaptyUIFlowView,
product: AdaptyPaywallProduct,
onStartPurchase: () -> Unit,
onFinishPurchase: () -> Unit,
) {
onStartPurchase() // the flow shows its loading indicator
scope.launch {
val transactionId = myPurchaseApi(product.vendorProductId)
Adapty.reportTransaction(
transactionId = transactionId,
variationId = product.paywallVariationId,
).onError { error ->
// handle the error
}
onFinishPurchase() // the flow hides the loading indicator
}
}
override fun observerModeDidInitiateRestore(
view: AdaptyUIFlowView,
onStartRestore: () -> Unit,
onFinishRestore: () -> Unit,
) {
onStartRestore()
scope.launch {
myRestoreApi()
onFinishRestore()
}
}
}
在展示任何流程之前,请先注册一次 resolver:
AdaptyUI.setObserverModeResolver(MyObserverModeResolver(scope))
参见在 Observer 模式下展示流程和在 Observer 模式下上报交易。
在您创建的每个流程视图上,通过 view.setEventHandlers 设置 onObserverPurchaseInitiated 和 onObserverRestoreInitiated —— Capacitor 没有单独的 resolver 对象,这两个处理程序是常规事件处理程序的组成部分。
const view = await createFlowView(flow);
const unsubscribe = await view.setEventHandlers({
onObserverPurchaseInitiated(product, onStartPurchase, onFinishPurchase) {
onStartPurchase(); // the flow shows its loading indicator
myPurchaseApi(product.vendorProductId)
.then((transactionId) =>
adapty.reportTransaction({ transactionId, variationId: product.variationId }),
)
.finally(() => onFinishPurchase()); // the flow hides the loading indicator
return false; // keep the flow open; dismiss it yourself after success
},
onObserverRestoreInitiated(onStartRestore, onFinishRestore) {
onStartRestore();
myRestoreApi().finally(() => onFinishRestore());
return false;
},
});
请参阅以 Observer 模式呈现流程和在 Observer 模式下上报交易。