我正在尝试将 React Native 应用程序集成到现有的 IOS 应用程序中。但我收到以下错误。
错误日志: *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[RCTCxxBridge devSettings]:无法识别的选择器发送到实例 0x14d3317c0” *** 首先抛出调用堆栈: (0x18b500f20 0x183386018 0x18b60a480 0x18b49dfb4 0x18b49d8d0 0x11232eaec 0x11232be48 0x111578b98 0x11157a7bc 0x11158ed18 0x11158 f4d8 0x1e83578f8 0x1e83540cc) libc++abi:由于未捕获的 NSException 类型异常而终止
反应:18.3.1
反应原生:0.74.2
示例代码来自:https://github.com/duytq94/demo-integrate-react-native/tree/master
RNViewManager.swift
import Foundation
import React
class RNViewManager: NSObject {
var bridge: RCTBridge?
static let sharedInstance = RNViewManager()
func createBridgeIfNeeded() -> RCTBridge {
if bridge == nil {
bridge = RCTBridge.init(delegate: self, launchOptions: nil)
}
return bridge!
}
func viewForModule(_ moduleName: String, initialProperties: [String : Any]?) -> RCTRootView {
let viewBridge = createBridgeIfNeeded()
let rootView: RCTRootView = RCTRootView(
bridge: viewBridge,
moduleName: moduleName,
initialProperties: initialProperties)
return rootView
}
}
extension RNViewManager: RCTBridgeDelegate {
func sourceURL(for bridge: RCTBridge!) -> URL! {
#if DEBUG
return URL(string: "http://192.168.1.25:8081/index.bundle?platform=ios")
#else
return Bundle.main.url(forResource: "main", withExtension: "jsbundle")
#endif
}
}
示例按钮点击代码:
let rootView = RNViewManager.sharedInstance.viewForModule(
"MyReactNativeApp",
initialProperties: ["message_from_native": "messageFromNative"])
let reactNativeVC = UIViewController()
reactNativeVC.view = rootView
reactNativeVC.modalPresentationStyle = .fullScreen
present(reactNativeVC, animated: true)