Detect app crashed during load / last time it was run?

耗尽温柔 提交于 2019-11-27 19:16:08

问题


I would like for my app to reset to standard settings if it crashed during startup. Preferably also if it crashed last time it was run.

EDIT: Crittercism has a crittercismDidCrashOnLastLoad method, but it only handles the case of crashing during load. It didn't work properly in the version of the library I used, but this has since been fixed.

Suggestions?


回答1:


Make 2 functions in your AppDelegate.m file:

void HandleException(NSException *exception) {
    NSLog(@"App crashing with exception: %@", exception);
    //Save somewhere that your app has crashed.
}

void HandleSignal(int signal) {
    NSLog(@"We received a signal: %d", signal);
    //Save somewhere that your app has crashed.
}

Then in your -(BOOL)application:didFinishLaunchingWithOptions before anything else put:

NSSetUncaughtExceptionHandler(&HandleException);

struct sigaction signalAction;
memset(&signalAction, 0, sizeof(signalAction));
signalAction.sa_handler = &HandleSignal;

sigaction(SIGABRT, &signalAction, NULL);
sigaction(SIGILL, &signalAction, NULL);
sigaction(SIGBUS, &signalAction, NULL);



回答2:


Using Crashlytics you then can set CrashlyticsDelegate to detect a crash on Swift or ObjC code.

import Fabric
import Crashlytics
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
    Crashlytics.sharedInstance().delegate = self
    Fabric.with([Crashlytics.self])
    return true
}

//MARK: - CrashlyticsDelegate

func crashlyticsDidDetectReport(forLastExecution report: CLSReport, completionHandler: @escaping (Bool) -> Void)
{
    completionHandler(true)
}

From Crashlytics docs

Your delegate must invoke the completionHandler, but does not need to do so synchronously, or even on the main thread. Invoking completionHandler with NO will cause the detected report to be deleted and not submitted to Crashlytics. This is useful for implementing permission prompts, or other more-complex forms of logic around submitting crashes.

Make certain that the delegate is setup before starting Crashlytics with startWithAPIKey:… or via [Fabric with:…]. Failure to do will result in missing any delegate callbacks that occur synchronously during start.



来源:https://stackoverflow.com/questions/10885313/detect-app-crashed-during-load-last-time-it-was-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!