xcode 检测应用程序在加载期间/上次运行时崩溃了吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10885313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:29:55  来源:igfitidea点击:

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

iosxcodecrittercism

提问by Anders Sewerin Johansen

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: Crittercismhas a crittercismDidCrashOnLastLoadmethod, 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.

编辑:Crittercism有一个crittercismDidCrashOnLastLoad方法,但它只处理加载过程中崩溃的情况。它在我使用的库版本中无法正常工作,但此后已修复。

Suggestions?

建议?

回答by graver

Make 2 functions in your AppDelegate.mfile:

在您的AppDelegate.m文件中创建2 个函数:

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:didFinishLaunchingWithOptionsbefore anything else put:

然后在你的-(BOOL)application:didFinishLaunchingWithOptions其他任何东西之前放:

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);

回答by Igor

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

然后使用 Crashlytics,您可以设置 CrashlyticsDelegate 来检测 Swift 或 ObjC 代码上的崩溃。

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

来自 Crashlytics 文档

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.

您的委托必须调用 completionHandler,但不需要同步执行,甚至不需要在主线程上执行。使用 NO 调用 completionHandler 将导致检测到的报告被删除并且不会提交给 Crashlytics。这对于实现权限提示或其他更复杂的提交崩溃的逻辑形式很有用。

确保在使用 startWithAPIKey:... 或通过 [Fabric with:...] 启动 Crashlytics 之前设置了委托。不这样做将导致丢失在启动期间同步发生的任何委托回调。