导出 Swift 原生模块给 React Native 使用,而目前 React Native 跨平台移动开发框架只有 Objc 版本的 API
1. 参考文章
- 将 React Native 集成到现有 Swift 原生应用
导出 Swift 原生模块给 React Native 使用
- https://reactnative.cn/docs/native-modules-ios/#从-swift-导出
https://github.com/jancywen/SwiftRnApp/tree/master/SwiftRnApp/RNFile
Swift 与 OC 互相调用
https://www.yuque.com/docs/share/859bcc9f-61d2-4a79-97fa-592a8548125e
Swift 调用 Sign in with Apple 接口
Keychain
UnsafeMutablePointer
- https://www.jianshu.com/p/e90393ba2aea
文件管理
OC 调用方法,携带多个参数值
Swift 调用 OC 的 Runtime
- https://stackoverflow.com/questions/36208155/
- https://blog.csdn.net/loveqcx123/article/details/76976053
2. 问题汇总
2.1 在 Podfile 中使用 use_frameworks!, React 编译后遇到的问题
‘RCTAnimation/RCTValueAnimatedNode.h’ file not found
‘fishhook/fishhook.h’ file not found
[方案]:将 #import
[参考]:链接1 链接2
Swift class extensions and categories on Swift classes are not allowed to have +load methods
[方案]:修改源码
[参考]:链接1 链接2
Undefined symbols for architecture x86_64:
“google::LogMessage::LogMessage(char const*, int, int)”, referenced from:
[方案]:在 React.podspec 添加缺失的依赖
[参考]:链接1 链接2
在文件 Yoga-internal.h 中报错:’algorithm’ file not found
[方案]:在 yoga.podspec 文件中,添加一行
spec.public_header_files = 'yoga/Yoga.h', 'yoga/YGEnums.h', 'yoga/YGMacros.h'
[参考]:链接
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 中还是得定义类名前缀