Detect App Crash on Launch

限于喜欢 提交于 2019-12-24 12:38:19

问题


I saw a couple applications that detect whether an app crashed the last time it was used to encourage the user to report the bug.

How can I do that? I tried this solution, but didn't get it to work in my swift project...
An approach I thought of is to save something each time the app is about to be closed and then read the value when launching the app, I guess when the app crashes then it won't be able to save anything anymore, right?
But, this is not very elegant. Is there a better way for detecting a crash?

Thanks in advance :)


回答1:


You could use an inline closure to perform you logging during crash.

NSSetUncaughtExceptionHandler { exception in
    // Do necessary logging work here
}

This SO thread might be of help to you..




回答2:


You could use crashlytics by twitter.

  1. Download fabric mac app
  2. Open it & sign up for a new account
  3. Add your xcode project to your list of projects in the fabric
  4. Select to add the crashlytics framework to your app
  5. Build your app
  6. See the given code snippet to start with crashlytics
  7. Visit Fabric documentation for further information



回答3:


import XCPlayground
import Foundation


let fm = NSFileManager()
if fm.fileExistsAtPath("terminated") {
    try! fm.removeItemAtPath("terminated")
} else {
    print("an app crashed last time")
}
// set i to nil to force the app to crash
// than set it to 0 and execute it again
let i: Int! = 0
let j: Int = i

fm.createFileAtPath("terminated", contents: nil, attributes: nil)
print("terminated")


来源:https://stackoverflow.com/questions/33844313/detect-app-crash-on-launch

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