override func awakeFromNib() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "crashedAlert", name: crashedNotificationKey, object: nil)
}
func crashedAlert() {
let alert = UIAlertController(title: "The app has crashed!", message: "Sorry about that! I am just a 17 year old highschooler making my first game!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "It's cool bro.", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
// 1. import Crashlytics
import FirebaseCrashlytics
class YOUR_ROOT_VIEW_CONTROLLER {
override viewDidLoad(){
//...
// 2. register to get the notifications
self.configCrashlytics()
// ...
}
func configCrashlytics() {
/* You must set setCrashlyticsCollectionEnabled to false in order to use
checkForUnsentReportsWithCompletion. */
Crashlytics.crashlytics().setCrashlyticsCollectionEnabled(false)
Crashlytics.crashlytics().checkForUnsentReports { hasUnsentReport in
let hasUserConsent = false
// ...get user consent.
if hasUserConsent && hasUnsentReport {
Crashlytics.crashlytics().sendUnsentReports()
} else {
Crashlytics.crashlytics().deleteUnsentReports()
}
}
// Detect when a crash happens during your app's last run.
if Crashlytics.crashlytics().didCrashDuringPreviousExecution() {
// ...notify the user that last time the app crashed
}
}
// 4. Add a button that run fatallError()
@IBAction func crashApp(_ sender: UIButton) {
fatalError()
}
}
4条答案
按热度按时间ycl3bljg1#
多亏了@RyanCollins的帮助,我自己解决了这个问题。App Delegate中的函数
applicationWillTerminate
仅在应用程序正确关闭时运行。原生检测应用崩溃的代码如下所示。全局定义变量
字符串
应用委托
型
根视图控制器
型
xwbd5t1u2#
详细数据
方案一
https://github.com/zixun/CrashEye/blob/master/CrashEye/Classes/CrashEye.swift
溶液1全样
!不要忘记复制(或安装POD)解决方案代码!!!
AppDelegate.swift
字符串
ViewController.swift
型
解决方案2. Crashlytics
Install Crashlytics的
溶液2全样品
AppDelegate.swift
型
ViewController.swift
型
结果
1.启动应用程序
1.按下“Booom”按钮(应用程序将崩溃)
1.又是午餐app
x1c 0d1x的数据
omhiaaxx3#
问题是,如果应用程序崩溃,那么它就无法运行代码来写入NSUserDefaults。
我所知道的最好的解决方案是使用PLCrashReporter(https://plcrashreporter.org)
83qze16e4#
FirebaseCrashlytics
如果您不使用
Fabric
,因为它已被弃用,您需要在您的ViewController
中使用以下代码,这些代码在Firebase Crashlytics SDK的更新中提到。我是这样用的:
字符串