1

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

Community
  • 1
  • 1
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
  • i recommend to use crash reporter like fabric & ... it's very easy to use and help you to find your problem easy – Mo Farhand Nov 21 '15 at 14:17
  • the solution from your link meats all your requirements. if the app crash, save the information. when run the app, check saved information and clear the saved context. why it didn't work in your app? – user3441734 Nov 21 '15 at 14:28
  • I think I was able to translate it but it complained about the thing with the `&` – LinusGeffarth Nov 21 '15 at 14:36
  • @LinusG. your idea is better. see my answer. even though you use NSSetUncaughtExceptionHandler, it is not useful with swift fatal errors, like an access to unwrapped nil ... – user3441734 Nov 21 '15 at 21:29
  • How are you detecting each time the app is about to be closed? appWillTerminate is not called every time, in all cases from my understanding – Keer Aug 07 '23 at 13:30
  • I don't recall. I believe there's something like "enters background" that would be suitable. @Keer – LinusGeffarth Aug 17 '23 at 10:07

3 Answers3

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..

Community
  • 1
  • 1
R P
  • 1,173
  • 2
  • 11
  • 20
  • this works only on UncaughtException, if you access unwrapped nil, the handler doesn't execute nothing ... – user3441734 Nov 21 '15 at 21:24
  • I agree, but Swift is designed with safety in mind by eliminating unsafe code during compile time. Optionals are supposed to be safely managed at all time through various mechanisms (like optional binding) provided by the language. IMHO, forced unwrapping/ implicit unwrapping should be avoided. – R P Nov 21 '15 at 21:54
  • 1
    in pure swift code there should be no uncaught exception at all ... in reality the app can crash with other than uncaught exception very easy. OP would like to know if the app crashed or not. may be 'not very elegant', but his method seems to be the only one available. – user3441734 Nov 21 '15 at 22:14
0

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
Wassim Seifeddine
  • 1,002
  • 12
  • 25
0
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")
user3441734
  • 16,722
  • 2
  • 40
  • 59