我需要在运送到其他应用程序的静态库中实现崩溃报告。为此,我将自己注册为NSUncaughtExceptionHandler
:
NSSetUncaughtExceptionHandler(HsWatchdogUncaughtExceptionHandler);
signal(SIGABRT, SignalHandler);
signal(SIGILL, SignalHandler);
signal(SIGSEGV, SignalHandler);
signal(SIGFPE, SignalHandler);
signal(SIGBUS, SignalHandler);
signal(SIGPIPE, SignalHandler);
在此之前,我还使用
NSGetUncaughtExceptionHandler()
保留了对先前UncaughtExceptionHandler的引用。由于使用我的库的许多应用程序也都有自己的崩溃报告机制(通常是Crashlytics),因此我需要我的lib才能很好地配合使用。
我打算做的是,在用
NSException
调用我的exceptionHandler之后,我想将其传递给“先前的UncaughtExceptionHandler”。这项工作会:
self.previousUncaughtExceptionHandler(exception);
最佳答案
声明一个变量以存储先前的处理程序
static NSUncaughtExceptionHandler *_previousHandler;
首先获取先前的处理程序并将其存储在全局变量中:
_previousHandler = NSGetUncaughtExceptionHandler();
然后创建自己的处理程序:
void onException(NSException * exception) {
// do what you want to do ... then call the previous handler
_previousHandler(exception);
}
将其设置为未捕获的异常处理程序:
NSSetUncaughtExceptionHandler(&onException)