ios - 反应本机错误窗口导致iOS崩溃
问题描述
我按照官方网站上的说明将 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
解决方案
使用相同的堆栈跟踪和错误遇到这个确切的问题。
如果您查看最上面的错误消息,它会抱怨 in 中的window
属性-[MyApp.AppDelegate window]
是 an unrecognized selector
,这意味着它在 MyApp.AppDelegate 中不存在。
从您发布的堆栈跟踪中,您可以看到 RCTRedBox 类正在尝试在此处调用它。
解决方案:
如果您将 UIWindowwindow
属性添加到您的 AppDelegate 类(MyApp.AppDelegate
在您的情况下),并将其设置为等于您的主窗口(即[UIScreen mainScreen]
或其他),它应该在尝试显示红框屏幕时停止抛出该错误。
推荐阅读
- string - 在 COBOL 中不使用 reverse 函数反转字符串
- c - 在字符串中分别打印偶数奇数字符
- python-3.x - 用中间空格格式化“键:值”字符串
- python - 语言翻译
- python - ./manage.py 使用 https 运行服务器
- javascript - for循环中的promise问题
- c# - 使用 Unity C# 和 PHP(Mysql 作为数据库)的动态争夺游戏
- android - Android Checkbox setOnCheckedChangeListener 不起作用
- java - 我如何制作像 DateField 这样的 JtextField,其中永久添加了 Slash(/)
- python - 将magicmock传递给jsonpickle.encode()时超出最大递归深度