我正在尝试为 React 0.61.2 创建一个自定义 native 模块。
我已经创建了 Android 版本,但是 iOS 似乎存在一些问题。
我正在尝试执行以下命令:
#import <React/RCTBridgeModule.h>
@interface PaypalSdk : NSObject <RCTBridgeModule>
@end
并得到:
'React/RCTBridgeModule.h' file not found
在早期的 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
最佳答案
代替:
#import <React/RCTBridgeModule.h>
您通常会在 上使用它来解决。 0.59x 版本:#if __has_include("RCTBridgeModule.h")
#import "RCTBridgeModule.h"
#else
#import <React/RCTBridgeModule.h>
#endif
然而,来自 RN >= v0.60x ,我们需要为提示找不到 React/{whatever.h} 的第 3 方/自定义库添加 .podspec 到 podfile。所以请,不要将 .a 文件添加到 Xcode 的 Linked Framework and Libraries
中.为了解决这个问题,我们可以开始做:
podspec
来自其他 3rd 方库(例如,RNDeviceInfo/etc..);podspec
通过替换一些项目(例如, s.name , s.source , s.source_files )s.name = "MyPayPalLib"
s.source = { :git => "https://github.com/my-super-repo/paypal-lib-repo.git", :tag => "v#{s.version}" }
.请按照以下步骤操作,注意您的库是托管在 npm/yarn/git(远程)上还是本地存储在项目的目录中。
.
A. 如果远程图书馆:
s.source_files = "ios/**/*.{h,m}"
pod 'MyPayPalLib', :path => '../node_modules/MyPayPalLib'
pod install
B. 如果本 map 书馆:
s.source
路径作为您的需要。 s.source = { :http => 'file:' + __dir__ + '/' }
s.source_files = "customLib/*.{h,m}"
pod 'MyPayPalLib', :path => '../localLibFolder/MyPayPalLib'
pod install
解决方案:
修复错误后:
'React/RCTBridgeModule.h' file not found in React Native 0.61.2
, NativeModules
对象为空。所以,经过一些研究,我们得到了这个问题:
s.source_files
prop 值错误!始终小心使用正确的
s.source_files
podspec Prop 。OP 正在使用:
s.source_files = "react-native-paypal-sdk/*.{h,m}"
但是,这应该是:s.source_files = "ios/**/*.{h,m}"
这是远程存储库的推荐方式。
关于objective-c - 在 React Native 0.61.2 中找不到“React/RCTBridgeModule.h”文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61348308/