迁移 Adapty React Native SDK 至 v3.14

Adapty React Native SDK 3.14.0 是一个主要版本,引入了一些需要你进行迁移操作的改进:

  • registerEventHandlers 方法已替换为 setEventHandlers 方法。
  • AdaptyOnboardingView 中,事件处理器现在以独立 props 的形式传入,而不再使用 eventHandlers 对象。
  • 为 UI 组件引入了全新的简化导入方式。
  • logShowOnboarding 方法已删除。
  • React Native 最低版本要求已更新至 0.73.0。
  • 付费墙和用户引导的 iOS 默认展示样式已从页面表单更改为全屏。

registerEventHandlers 替换为 setEventHandlers

用于 Adapty 付费墙编辑工具和用户引导编辑工具的 registerEventHandlers 方法已被替换为 setEventHandlers 方法。 如果你使用了 Adapty 付费墙编辑工具和/或 Adapty 用户引导编辑工具,请在应用代码中找到 registerEventHandlers 并将其替换为 setEventHandlers

此更改旨在让方法行为更加清晰:每个处理器返回 true/false,因此处理器采用单一生效的方式运行,为同一事件注册多个处理器会导致最终行为难以预测。 请注意,使用 AdaptyOnboardingViewAdaptyPaywallView 等 React 组件时,无需在事件处理函数中返回 true/false,因为你可以通过自己的状态管理来控制组件的显示与隐藏。返回值仅在以模态框形式展示页面时才需要,此时 SDK 负责管理视图的生命周期。

多次调用 setEventHandlers 会覆盖你已设置的处理函数,替换掉这些特定事件的默认处理函数及之前设置的处理函数。

- const unsubscribe = view.registerEventHandlers({
-    // your event handlers
- })

 const unsubscribe = view.setEventHandlers({
    // your event handlers
 })

更新 UI 组件的导入路径

Adapty SDK 3.14.0 引入了更简洁的 UI 组件导入方式。现在你可以直接从 react-native-adapty 导入,无需再从 react-native-adapty/dist/ui 导入。

新的导入方式更符合标准的 React Native 使用习惯,也让导入语句更加简洁。如果你正在使用 AdaptyPaywallViewAdaptyOnboardingView 等 UI 组件,请按照以下示例更新你的导入语句:

- import { AdaptyPaywallView } from 'react-native-adapty/dist/ui';
+ import { AdaptyPaywallView } from 'react-native-adapty';

- import { AdaptyOnboardingView } from 'react-native-adapty/dist/ui';
+ import { AdaptyOnboardingView } from 'react-native-adapty';

- import { createPaywallView } from 'react-native-adapty/dist/ui';
+ import { createPaywallView } from 'react-native-adapty';

- import { createOnboardingView } from 'react-native-adapty/dist/ui';
+ import { createOnboardingView } from 'react-native-adapty';

为保持向后兼容性,旧的导入方式(react-native-adapty/dist/ui)仍然受支持。但我们建议使用新的导入方式,以保持一致性和清晰度。

更新 React 组件中的用户引导事件处理器

用户引导的事件处理器已从 AdaptyOnboardingVieweventHandlers 对象中移出。如果你正在使用 AdaptyOnboardingView 展示用户引导,请更新事件处理结构。

请注意我们推荐的事件处理器实现方式。为避免每次渲染时重新创建对象,请对处理事件的函数使用 useCallback

 import React, { useCallback } from 'react';
- import { AdaptyOnboardingView } from 'react-native-adapty/dist/ui';
+ import { AdaptyOnboardingView } from 'react-native-adapty';
+ import type { OnboardingEventHandlers } from 'react-native-adapty';
+
+ function MyOnboarding({ onboarding }) {
+   const onAnalytics = useCallback<OnboardingEventHandlers['onAnalytics']>((event, meta) => {}, []);
+   const onClose = useCallback<OnboardingEventHandlers['onClose']>((actionId, meta) => {}, []);
+   const onCustom = useCallback<OnboardingEventHandlers['onCustom']>((actionId, meta) => {}, []);
+   const onPaywall = useCallback<OnboardingEventHandlers['onPaywall']>((actionId, meta) => {}, []);
+   const onStateUpdated = useCallback<OnboardingEventHandlers['onStateUpdated']>((action, meta) => {}, []);
+   const onFinishedLoading = useCallback<OnboardingEventHandlers['onFinishedLoading']>((meta) => {}, []);
+   const onError = useCallback<OnboardingEventHandlers['onError']>((error) => {}, []);
+
   return (
     <AdaptyOnboardingView
       onboarding={onboarding}
       style={styles.container}
-       eventHandlers={{
-         onAnalytics(event, meta) { /* ... */ },
-         onClose(actionId, meta) { /* ... */ },
-         onCustom(actionId, meta) { /* ... */ },
-         onPaywall(actionId, meta) { /* ... */ },
-         onStateUpdated(action, meta) { /* ... */ },
-         onFinishedLoading(meta) { /* ... */ },
-         onError(error) { /* ... */ },
-       }}
+       onAnalytics={onAnalytics}
+       onClose={onClose}
+       onCustom={onCustom}
+       onPaywall={onPaywall}
+       onStateUpdated={onStateUpdated}
+       onFinishedLoading={onFinishedLoading}
+       onError={onError}
     />
   );
+ }

为了向后兼容,eventHandlers prop 仍受支持,但已被弃用。我们建议迁移到如上所示的独立事件处理器 props。

删除 logShowOnboarding

在 Adapty SDK 3.14.0 中,我们已从 SDK 中删除了 logShowOnboarding 方法。 如果您一直在使用此方法,将 SDK 升级到 3.14 或更高版本后,该方法将不再可用。

作为替代,您可以在 Adapty 无代码用户引导编辑工具中创建用户引导。这些用户引导的分析数据会自动追踪,并且您有大量的自定义选项。

更新 React Native

从 Adapty SDK 3.14.0 开始,React Native 的最低支持版本为 0.73.0。如果您使用的是较早版本,请将 React Native 更新至 0.73.0 或更高版本,以确保您使用 Adapty SDK 的体验保持一致和可靠。

更新模态付费墙和用户引导的 iOS 呈现样式

在 Adapty SDK 3.14.0 中,使用 view.present() 方法显示的付费墙和用户引导的默认 iOS 呈现样式已从页面表单更改为全屏。

如果您想保留之前的页面表单呈现样式,请将 iosPresentationStyle 参数传递给 present() 方法:

try {
  await view.present({ iosPresentationStyle: 'page_sheet' });
} catch (error) {
  // handle the error
}