繁体   English   中英

在Obj C中捕获React Native错误

[英]Catching React Native error in Obj C

我希望防止React Native应用程序中任何意外引发的异常使整个应用程序崩溃。

有什么方法可以处理obj c端在React Native JS中引发的异常。 由于该过程超出了App Delegate的范围,因此简单的@try/@catch根本无法解决问题。

我已经实现了NSSetUncaughtExceptionHandler ,它已经设置为提供故障堆栈跟踪,但是就优雅地处理RN问题而言,它NSSetUncaughtExceptionHandler

我必须进行大量研究,因为它的文献记录很少。 最后,我的一个朋友帮助我解决了这个问题。

你需要设置

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

另外,创建一个RCTExceptionsManager实例,并按照RCTBridgeDelegate协议将其传递到您类的extraModulesForBridge方法中。

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

您需要实现的两种方法是:

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

这使您可以完全控制致命的异常处理,并对本机崩溃做出反应不会使应用程序崩溃。

暂无
暂无

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

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