React Native SDK 故障排除

重要

Visual Studio App Center 于 2025 年 3 月 31 日停用,但分析和诊断功能除外,这些功能将继续受支持,直到 2026 年 6 月 30 日。 了解详细信息

找不到“React/RCTDefines.h”文件

如果未正确引用 RN 核心库,则会出现此错误,这可能会导致不同类型的集成或链接问题。 在 Podfile 中使用相对路径(而不是静态在项目中)链接依赖项时,通常会发生这种情况。

我们的链接脚本仅支持链接 Pod 的标准方法,因此要解决此问题:

  1. 将文件中 Podfile 的依赖项替换为相对链接路径:

    以前:

    pod 'AppCenter/Analytics', '~> 1.14.0'
    pod 'AppCenter/Crashes', '~> 1.14.0'
    pod 'AppCenterReactNativeShared', '~> 1.13.0'
    

    之后:

    pod 'appcenter', path: '../node_modules/appcenter/ios'
    pod 'appcenter-analytics', path: '../node_modules/appcenter-analytics/ios'
    pod 'appcenter-crashes', path: '../node_modules/appcenter-crashes/ios'
    
  2. pod install文件夹运行iOS

  3. AppDelegate.m 中,替换导入:

    以前:

    #import <AppCenterReactNative/AppCenterReactNative.h>
    #import <AppCenterReactNativeAnalytics/AppCenterReactNativeAnalytics.h>
    #import <AppCenterReactNativeCrashes/AppCenterReactNativeCrashes.h>
    

    之后:

    #import "AppCenterReactNative.h"
    #import "AppCenterReactNativeAnalytics.h"
    #import "AppCenterReactNativeCrashes.h"
    
  4. 从项目中删除 AppCenter 依赖项(右键单击依赖项 > 删除依赖项)。

    • 选择 <YOUR_APP> ->Libraries 并删除下一个文件:
      • AppCenterReactNative.xcodeproj
      • AppCenterReactNativeAnalytics.xcodeproj
      • AppCenterReactNativeCrashes.xcodeproj
    • 打开项目设置,在“链接框架和库”部分中的“常规”选项卡下,删除引用上一步中删除的目标库的新项:
      • libAppCenterReactNative.a
      • libAppCenterReactNativeAnalytics.a
      • libAppCenterReactNativeCrashes.a
    • 修改 标头搜索路径 以从 AppCenter React Native 插件项目中删除标头。 打开项目设置,在“标头搜索路径”部分中的“生成设置”选项卡下,删除头文件的以下位置:
      • $(SRCROOT)/../node_modules/appcenter/ios/AppCenterReactNative
      • $(SRCROOT)/../node_modules/appcenter-analytics/ios/AppCenterReactNativeAnalytics
      • $(SRCROOT)/../node_modules/appcenter-crashes/ios/AppCenterReactNativeCrashes

如果在您的计算机上安装了yarn,则react-native init {myapp}会使用yarn而不是npm来初始化您的应用程序并安装依赖项。 如果在运行react-native link时看到以下错误消息,很可能是因为 App Center 依赖项是从npm install {package}安装的,而不是从yarn add {package}安装的,因此在安装过程中npmyarn被混合了。

Command `link` unrecognized. Make sure that you have run `npm install` and that you are inside a react-native project.

在这种情况下,请运行 npm install 并重试 react-native link

生成错误,例如未找到“AppCenterCrashes/MSACErrorReport.h”文件

React-Native 0.60 及更高

可能原因是由于我们的 Apple SDK 中的重大改动导致主要包版本之间的冲突。

  1. 确保所有 appcenter 包都使用相同的主版本。 如有需要,使用npm installyarn更新它们。

  2. 转到项目的 ios 文件夹并删除 Podfile.lockPods 文件夹。

  3. pod install --repo-update 文件夹中运行

  4. 验证 Podfile.lock 是否包含 appcenter 同一主版本的依赖项。

React Native 低于 0.60

一个可能的原因是在没有react-native link 的情况下运行

若要确认原因、执行 react-native link并在日志中检查以下行:

Could not configure AppCenter for iOS. Error Reason - spawn pod ENOENT

此行位于日志的以下部分:

Added code to initialize iOS AppCenter SDK in ios/TestApp/AppDelegate.m
Installing Cocoapods dependencies...
Could not configure AppCenter for iOS. Error Reason - spawn pod ENOENT
rnpm-install info Platform 'ios' module appcenter-analytics is already linked
rnpm-install info Platform 'android' module appcenter-analytics is already linked

如果看到此错误,请确保pod来自 CocoaPods 的命令在系统的 PATH 环境变量中可用。

修复 CocoaPods 安装后,在 pod install 文件夹中运行以修复项目。

如果在react-native link期间看到以下 CocoaPods 错误消息,那么您计算机上的 ~/.cocoapods/repos的本地克隆()可能不是最新的。

Analyzing dependencies [!] Unable to find a specification for AppCenterReactNativeShared (~> {version})

运行 pod repo update 以更新规范存储库,然后重试 react-native link 。 有关 CocoaPods 命令的详细信息,请参阅 CocoaPods 命令行参考

如何使用 App Center SDK 将应用从 React Native 0.59 升级到 0.60

若要将 React Native 升级到版本 0.60.0,请使用以下命令:

react-native upgrade 0.60.0

注释

如果在升级过程中收到此错误 Command failed: git status -s fatal: not a git repository (or any of the parent directories): .git,请执行以下步骤:

git init
git add .
git commit -m "Upgrade react-native"

完成升级后,可以删除目录 .git

更新 React Native iOS

  1. 打开 Podfile 并替换以下行上的 App Center 依赖项:

    use_native_modules!
    
  2. 运行下面的命令:

    pod repo update
    

如何将 React Native SDK 更新到最新版本

如果已将 SDK 集成到应用程序中,并且想要升级到较新版本的 SDK,请执行以下步骤进行升级:

  1. npm uninstall --save appcenter appcenter-analytics appcenter-crashes 以卸载旧的 App Center 包。

  2. npm install --save-exact appcenter appcenter-analytics appcenter-crashes 安装最新的 App Center 软件包。

  3. pod repo update 确保 CocoaPods 规范存储库是最新的。

  4. react-native link

小窍门

如果在 Ios react-native link文件夹中看到类似于以下 CocoaPods 错误消息的错误消息,请删除 ios 文件夹中自动生成的 Podfile.lock,然后再次运行react-native link

[!] Unable to satisfy the following requirements:

- `AppCenter/Core (= 1.0.0)` required by `Podfile.lock`
- `AppCenter/Core (= 1.0.0)` required by `AppCenter/Crashes (1.0.0)`
- `AppCenter/Core (= 1.0.0)` required by `AppCenter/Analytics (1.0.0)`
- `AppCenter/Core (= 1.0.1)` required by `AppCenterReactNativeShared (1.0.1)`

从 jCenter 迁移到 Maven Central

由于 jCenter 支持终止 ,所有程序集都已移动到 Maven Central 存储库。 若要使用 App Center,需要添加到 mavenCentral() Gradle 文件中的存储库,如下所示:

repositories {
   google()
   mavenCentral()
}

保护 App Center 机密值

这是 app_secret 应用的标识符,需要知道流量应用于哪些应用,并且不能用于检索或编辑现有数据。 如果暴露了 app_secret 数据,最大的风险是向应用发送不良数据,但它不会对数据的安全性产生影响。

若要检索任何敏感数据,需要提供在客户端生成的应用/用户 令牌。 无法使客户端上的数据完全安全。

可以通过使用环境变量将应用机密注入代码来提高应用的安全性。 这样,密码在代码中不可见。