简体   繁体   English

在Obj C中捕获React Native错误

[英]Catching React Native error in Obj C

I am looking to prevent any unexpected raised Exceptions in my React Native app from crashing the entire app. 我希望防止React Native应用程序中任何意外引发的异常使整个应用程序崩溃。

Is there any way to handle exceptions raised in the React Native JS on the obj c side. 有什么方法可以处理obj c端在React Native JS中引发的异常。 Since the process extends beyond the App Delegate, a simple @try/@catch simply doesn't cut it. 由于该过程超出了App Delegate的范围,因此简单的@try/@catch根本无法解决问题。

I already have implemented NSSetUncaughtExceptionHandler which I have set up to provide a failure stack trace, but it falls short in terms of gracefully handling RN Issues 我已经实现了NSSetUncaughtExceptionHandler ,它已经设置为提供故障堆栈跟踪,但是就优雅地处理RN问题而言,它NSSetUncaughtExceptionHandler

I had to research a lot because it's very poorly documented. 我必须进行大量研究,因为它的文献记录很少。 In the end, a friend of mine helped me out with this problem. 最后,我的一个朋友帮助我解决了这个问题。

You need to set 你需要设置

//This will tell react native that you are taking responsibility of fatal crashes.
RCTSetFatalHandler(^(NSError *error) {}); 

Also, create an RCTExceptionsManager instance and pass it in the extraModulesForBridge method of your class abiding by RCTBridgeDelegate protocol. 另外,创建一个RCTExceptionsManager实例,并按照RCTBridgeDelegate协议将其传递到您类的extraModulesForBridge方法中。

- (NSArray *)extraModulesForBridge:(RCTBridge *)bridge {
    return @[[RCTExceptionsManager alloc] initWithDelegate:self];
}

The two methods that you need to implement are: 您需要实现的两种方法是:

 - (void)handleSoftJSExceptionWithMessage:(NSString *)message stack:(NSArray *)stack exceptionId:(NSNumber *)exceptionId;
 - (void)handleFatalJSExceptionWithMessage:(NSString *)message stack:(NSArray *)stack exceptionId:(NSNumber *)exceptionId;

This gives you full control of the fatal exception handling and react native crashes won't crash the app. 这使您可以完全控制致命的异常处理,并对本机崩溃做出反应不会使应用程序崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM