第一步是安装Tapjoy React Native Plugin. 我们支持NPM和Yarn.
npm install tapjoy-react-native-sdk
yarn add tapjoy-react-native-sdk
您可以在您的应用中导入plugin:
import {Tapjoy, TJPlacement} from 'tapjoy-react-native-sdk'
ACCESS_WIFI_STATE
权限是可选的,可以将其添加到您的manifest:
<manifest ...>
...
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
...
</manifest>
调用connect意味着初始化Tapjoy SDK。建议在您的应用启动时调用初始化。
try {
let sdkKey = Platform.OS === 'ios' ? 'ios-sdk-key' : 'android-sdk-key'
let flags: object = {TJC_OPTION_USER_ID: 'userId'};
await Tapjoy.connect(sdkKey, flags);
} catch (error: any) {
let errorString = `Tapjoy SDK failed to connect. code: ${error.code}, message: ${error.message}`;
}
SDK v13.4.0支持connectWarning
回调。当连接期间出现非阻塞问题时,此回调将触发,connectSuccess
也会在之后触发。 目前,此功能仅检测在ConnectFlags中设置UserId的问题。
import {
NativeEventEmitter,
NativeModules,
} from 'react-native';
const TJ = NativeModules.TapjoyReactNativeSdk;
const TapjoyEmitter = new NativeEventEmitter(TJ);
const TapjoyEventType = 'Tapjoy';
const subscription = TapjoyEmitter.addListener(
TapjoyEventType,
(event: TapjoyEvent) => {
if (event.name === TJConnect.TJC_CONNECT_WARNING) {
subscription.remove();
setStatusLabelText(
`Tapjoy SDK connected with Warning: ErrorCode: ${event.code} ${event.message} `
);
}
}
);
现在您就可以尝试编译运行您的应用了。
在Tapjoy控制面板,您点击在顶层导航栏中“分析”,在左侧菜单栏中点击“实时”,您可以看到您应用运行相关的活动信息。
如果您的应用使用 App Tracking Transparency, 来请求使用IDFA,您需要在Info.plist配置NSUserTrackingUsageDescription key和自定义的描述信息。
然后安装react-native-tracking-transparency
package:
yarn add react-native-tracking-transparency
导入并展示请求对话框:
import {
getTrackingStatus,
requestTrackingPermission,
} from 'react-native-tracking-transparency';
...
let trackingStatus = await getTrackingStatus();
if (trackingStatus === 'authorized' || trackingStatus === 'unavailable') {
await Tapjoy.connect(sdkKey, flags);
}else{
trackingStatus = await requestTrackingPermission();
await Tapjoy.connect(sdkKey, flags);
}