导出 Swift 原生模块给 React Native 使用,而目前 React Native 跨平台移动开发框架只有 Objc 版本的 API

1. 参考文章

2. 问题汇总

2.1 在 Podfile 中使用 use_frameworks!React 编译后遇到的问题

‘RCTAnimation/RCTValueAnimatedNode.h’ file not found
‘fishhook/fishhook.h’ file not found
[方案]:将 #import 改成 #import “header.h” 即可
[参考]:链接1 链接2

1.png

2.png

Swift class extensions and categories on Swift classes are not allowed to have +load methods
[方案]:修改源码
[参考]:链接1 链接2

3.png

4.png

Undefined symbols for architecture x86_64:
“google::LogMessage::LogMessage(char const*, int, int)”, referenced from:
[方案]:在 React.podspec 添加缺失的依赖
[参考]:链接1 链接2

5.png

6.png

在文件 Yoga-internal.h 中报错:’algorithm’ file not found
[方案]:在 yoga.podspec 文件中,添加一行

  1. spec.public_header_files = 'yoga/Yoga.h', 'yoga/YGEnums.h', 'yoga/YGMacros.h'

[参考]:链接

7.png

2.2 编码问题

The process has been returned to the state before expression evaluation.
[方案]:注意数据类型/格式

The callback login() exists in module GameCenter, but only one callback may be registered to a function in a native module.
[说明]:这是由于在一个事件中连续发送了两次callback导致的
[方案]:链接

Illegal callback invocation from native module. This callback type only permits a single invocation from native code.
[说明]:也是由于在一个事件中连续发送了两次callback导致的
[方案]:链接

2.3 其他问题

将RNGFObjcSwiftMixed 推到 CocoaPods 私有库遇到的报错:

- ERROR | [iOS] unknown: Encountered an unknown error (The following Swift pods cannot yet be integrated as static libraries:

The Swift pod RNGFObjcSwiftMixed depends upon React, which does not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set use_modular_headers! globally in your Podfile, or specify :modular_headers => true for particular dependencies.

[方案]:链接
pod repo push —help
pod repo push RNGFSpecs —allow-warnings —verbose —use-libraries —use-modular-headers

在模拟器运行安装时,报错:Frameworks/folly.framework; Extra info about plist: ACL=
[方案]:删除项目所有与 pod 相关的资源文件,重新 install 就好了

3. 注意事项

虽然 Swift 有命名空间,但是在 OC 中还是得定义类名前缀