ios - React native 错误窗口导致 iOS 崩溃

标签 ios react-native

我按照官方网站上的说明将 React Native 集成到我现有的 iOS 应用程序中。
它大部分时间都可以工作,但是当出现 Js 错误并且它应该显示 React Native 红色错误窗口时,应用程序反而崩溃了。
我使用 RN 0.59

有什么我想念的吗?

这是我在应用程序中添加 React Native 的代码:

显示 RCTRootView 的 View Controller :

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

最佳答案

使用相同的堆栈跟踪和错误遇到了这个确切的问题。
如果您查看第一行错误消息,就会发现 window -[MyApp.AppDelegate window] 中的属性是 unrecognized selector ,这意味着它不存在于 MyApp.AppDelegate 中。
从您发布的堆栈跟踪中,您可以看到 RCTRedBox 类正在尝试调用该 here.
解决方案:
如果你添加一个 UIWindow window属性到您的 AppDelegate 类(在您的情况下为 MyApp.AppDelegate),并将其设置为等于您的主窗口(即 [UIScreen mainScreen] 或其他内容),它应该在尝试显示红框屏幕时停止抛出该错误。

关于ios - React native 错误窗口导致 iOS 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55916684/

相关文章:

ios - Swift 在我的自定义类中将值设置为枚举

ios - UITabBarController 等待加载 UIViewController 直到选择 Xamarin

react-native - Google Sheets API 与 React Native

react-native - react native : Fix TextInput at the bottom of the screen with a ScrollView

iphone - 为什么在 iOS5 中用中文创建 framesetter 非常慢

ios - 如何通过 iMessage ios 8 发送音频文件

ios - OKta + react native + OKta 自定义登录页面 + okta 离线登录

ios - 创建 Siri 快捷方式后在 Siri 中推荐快捷方式 - React Native

android - react native Flatlist renderItem 不更新

ios - 从 View Controller 的文本字段获取值