首页 > 解决方案 > 无法读取未定义的属性“initSdk”-Appsflyer 集成 React Native

问题描述

我正在尝试在反应本机应用程序中实现 AppsFlyer,但出现此错误“无法读取未定义的属性 'initSdk'”

我导入了 react-native-appsflyer

import React, { Component } from 'react';
import {
 Alert,
 Platform,
 AppRegistry,
 NetInfo,
 Text,
} from 'react-native';
//...

import appsFlyer from 'react-native-appsflyer';

//...

并尝试调用initSdk方法

export default class  App extends Component {

    initSdk(){
        console.log('allo appsflyer');

        let options = {
          devKey:  'AF_DEV_KEY',
          appId: "IOS_APP_ID",
          isDebug: true
        };

       appsFlyer.initSdk(options,
           (result) => {
             this.setState( { ...this.state, initSdkResponse: result });
             console.log(initSdkResponse);
           },
           (error) => {
             console.error(error);
           }
           )
     }

并在我的 startApp 函数中启动它

    startApp(root) {
        this.initSdk();
        console.log('app store update --> root', root);

        switch (root) {
          //...  
        }
    }
}

有人帮我吗?

标签: javascriptreactjsreact-nativeappsflyer

解决方案


react-native link react-native-appsflyer如果未正确链接本机依赖项,​​则可能会出现此错误,如果您从项目的根目录运行(对于 Android 本机依赖项)并为 iOS 添加相关的 Pod 依赖项,则应解决此错误:

将 appsFlyerFramework 添加到 Podfile 并运行 pod install。例子:

pod 'react-native-appsflyer',
:path => '../node_modules/react-native-appsflyer'

(假设您的 Podfile 位于 iOS 目录中)运行pod install

如果您不使用 cocoapods,您可以按照手动集成说明进行操作:https ://github.com/AppsFlyerSDK/react-native-appsflyer#manual-integration-integrating-without-cocoapods


推荐阅读