[英]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.