在 React Native 0.61.2 中找不到“React/RCTBridgeModule.h”文件

Cec*_*uez 7 xcode objective-c react-native

我正在尝试为 React 0.61.2 创建自定义本机模块。

我已经创建了 Android 版本,但是 iOS 似乎存在一些问题。

我正在尝试执行以下命令:

#import <React/RCTBridgeModule.h>

@interface PaypalSdk : NSObject <RCTBridgeModule>

@end
Run Code Online (Sandbox Code Playgroud)

并得到:

'React/RCTBridgeModule.h' file not found
Run Code Online (Sandbox Code Playgroud)

在早期的 React Native .6 版本中,它的工作方式似乎发生了很多变化。

在另一个较早版本的 React 中,我将 React.xcodeproj 添加到我的库中并且有效,但是这似乎不是一个选项,因为 React.xcodeproj 已被删除。

我正在解决这个问题,并测试了一切,但这些东西似乎都不起作用:

https://github.com/facebook/react-native/issues/25838

我也在这里尝试了所有方法,包括从我的方案中删除 React 并重新添加它:

https://github.com/facebook/react-native/issues/24363

这可能是什么原因造成的?

编辑:我的 PaypalSdk.m 文件的一部分:

#import "PaypalSdk.h"
#import <PayPal-iOS-SDK/PayPalMobile.h>
#import <PayPal-iOS-SDK/PayPalConfiguration.h>
#import <PayPal-iOS-SDK/PayPalOAuthScopes.h>
#import <PayPal-iOS-SDK/PayPalProfileSharingViewController.h>
#import <QuartzCore/QuartzCore.h>


@implementation PaypalSdk

... various methods and properties here

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(generateCode:(NSString *)code callback:(RCTResponseSenderBlock)callback)
{
... more code here
    callback(@[[NSString stringWithFormat: @"code: %@", "test"]]);
}

@end
Run Code Online (Sandbox Code Playgroud)

May*_*ita 3

代替:

#import <React/RCTBridgeModule.h>
Run Code Online (Sandbox Code Playgroud)

您通常会在0.59x版本上使用此解决方案:

#if __has_include("RCTBridgeModule.h")
#import "RCTBridgeModule.h"
#else
#import <React/RCTBridgeModule.h>
#endif
Run Code Online (Sandbox Code Playgroud)

然而,从 RN >= v0.60x开始,我们需要将 .podspec 添加到 podfile,以查找抱怨无法找到 React/{whatever.h} 的第 3 方/自定义库。因此,请不要将 .a 文件添加到 Xcode 的 .a 文件中Linked Framework and Libraries

为了解决这个问题,我们可以开始这样做:

  1. 从其他第 3 方库复制podspec(例如,RNDeviceInfo/等);

  2. podspec通过替换一些项目(例如s.names.sources.source_files)来创建自己的

s.name   = "MyPayPalLib"
s.source = { :git => "https://github.com/my-super-repo/paypal-lib-repo.git", :tag => "v#{s.version}" }
Run Code Online (Sandbox Code Playgroud)

按照接下来的步骤操作,请注意您的库是否托管在 npm/yarn/git(远程)上或本地存储在项目的目录中。

A. 如果是远程库:

  1. 添加以下内容以跟踪所有 lib 文件: s.source_files = "ios/**/*.{h,m}"

  2. 将新库添加到 Podfile: pod 'MyPayPalLib', :path => '../node_modules/MyPayPalLib'

  3. 做一个pod install

B. 如果是本地图书馆:

  1. 在同一自定义库路径中创建此 custom.podspec,否则您需要s.source根据需要修改上述路径。
s.source       = { :http => 'file:' + __dir__ + '/' }
s.source_files = "customLib/*.{h,m}"
Run Code Online (Sandbox Code Playgroud)
  1. 将新库添加到 Podfile: pod 'MyPayPalLib', :path => '../localLibFolder/MyPayPalLib'

  2. 做一个pod install


解决方案:

修复错误后:'React/RCTBridgeModule.h' file not found in React Native 0.61.2,该NativeModules对象为空。

因此,经过一番研究,我们发现了问题:该s.source_files道具的值错误!

请务必小心使用正确的s.source_filespodspec 属性。

OP正在使用:s.source_files = "react-native-paypal-sdk/*.{h,m}"

然而,这应该是:s.source_files = "ios/**/*.{h,m}"

这是远程存储库的推荐方式。