我按照官方网站上的说明将React Native集成到了现有的iOS应用程序中。 它在大多数情况下都有效,但是当出现Js错误并且应显示React Native红色错误窗口时,应用程序将崩溃。 我使用RN 0.59
有什么我想念的吗?
这是我的代码,用于在应用程序中添加React Native:
显示RCTRootView的视图控制器:
class OnboardingViewController : UIViewController {
var onboardingView: RCTRootView!
override func viewDidLoad() {
super.viewDidLoad()
self.onboardingView = ReactNativeBridge.instance.viewForModule("Onboarding", initialProperties: ["locale" : "language_http_key".localize()])
self.onboardingView.backgroundColor = UIColor.bbgBrandGreenColor()
self.view.addSubview(self.onboardingView)
if ((self.onboardingView) != nil) {
self.onboardingView.frame = self.view.bounds
}
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}
ReactNativeBridge实现:
class ReactNativeBridge: NSObject {
static let instance = ReactNativeBridge()
var bridge: RCTBridge?
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 ReactNativeBridge: RCTBridgeDelegate {
func sourceURL(for bridge: RCTBridge!) -> URL! {
#if DEBUG
return URL(string: "http://localhost:8081/index.ios.bundle?platform=ios")
#else
return Bundle.main.url(forResource: "main", withExtension: "jsbundle")
#endif
}
}
崩溃日志:
2019-04-30 10:09:30.173593+0200 MyApp[98998:6032042] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyApp.AppDelegate window]: unrecognized selector sent to instance 0x60000076c5f0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010eeb26fb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x000000010d8ccac5 objc_exception_throw + 48
2 CoreFoundation 0x000000010eed0ab4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 UIKitCore 0x0000000112945c3d -[UIResponder doesNotRecognizeSelector:] + 287
4 CoreFoundation 0x000000010eeb71a1 ___forwarding___ + 769
5 CoreFoundation 0x000000010eeb9238 _CF_forwarding_prep_0 + 120
6 React 0x000000010bc6e3d8 -[RCTRedBoxWindow bottomSafeViewHeight] + 120
7 React 0x000000010bc6ca61 -[RCTRedBoxWindow initWithFrame:] + 625
8 React 0x000000010bc718d1 __55-[RCTRedBox showErrorMessage:withParsedStack:isUpdate:]_block_invoke + 497
9 libdispatch.dylib 0x0000000110644d7f _dispatch_call_block_and_release + 12
10 libdispatch.dylib 0x0000000110645db5 _dispatch_client_callout + 8
11 libdispatch.dylib 0x0000000110653080 _dispatch_main_queue_callback_4CF + 1540
12 CoreFoundation 0x000000010ee198a9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
13 CoreFoundation 0x000000010ee13f56 __CFRunLoopRun + 2310
14 CoreFoundation 0x000000010ee13302 CFRunLoopRunSpecific + 626
15 GraphicsServices 0x0000000115c682fe GSEventRunModal + 65
16 UIKitCore 0x0000000112919ba2 UIApplicationMain + 140
17 MyApp 0x000000010756f658 main + 72
18 libdyld.dylib 0x00000001106ba541 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException