问题
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.
- Download fabric mac app
- Open it & sign up for a new account
- Add your xcode project to your list of projects in the fabric
- Select to add the crashlytics framework to your app
- Build your app
- See the given code snippet to start with crashlytics
- 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