0

In my Universal-App i have some issues where the app crashes. I could not reproduce the cause of the crash so i want at least make the app "save" so that it can restart again if it has crashed. Problem is, if my app crashes, it can happen that it cannot be started again due to some messed up user defaults. So, if i could delete my user defaults after my app has crashed while starting, the user could at least use the app furthermore.

So far for my goals. Now i only need to know how i can detect and save it, when my app has crashed during starting.

My first intention was to set a flag in user defaults when the app crashes and then ,if the flag is set, to reset the user defaults. But i don't know if - (void)applicationWillTerminate:(UIApplication *)application
will be called when my app crashes. And even if it is called. How can i detect if it crashed during starting?

Any suggestions would be welcome.

Greetings and thx in advance Maverick1st

Maverick1st
  • 3,774
  • 2
  • 34
  • 50
  • `-applicationWillTerminate:` won't be called if your app crashes. A crash is a hard stop in execution, not a controlled termination. (In fact, unless you're suppressing multitasking, that method will basically never be called in iOS 4+) – Ben Zotto Jun 07 '11 at 14:28
  • this link may help you. http://stackoverflow.com/questions/15423138/is-there-a-way-to-tell-if-an-ios-app-is-closed-unexpectedlycrash-force-close?lq=1 – Md.shohrab hossain Chowdhury May 30 '13 at 09:53

3 Answers3

6

What about setting a flag every time your app starts up and unsetting it when it terminates normally? That way you know if it is already set when the app starts, it crashed last time.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Great Idea, thx. I think you were a little bit earlier with your answer so i'll mark yours as correct answer. Even though the answer of WalkyTalky is also correct. But i voted your two answers up anyways. :) – Maverick1st Jun 07 '11 at 15:04
  • @Maverick1st: I beat @walkytalky by about 20 seconds :-) – JeremyP Jun 07 '11 at 15:07
4

One of the things about abnormal exit is that is going to be, well, abnormal. It's usually not a great idea to rely on being able to do anything sensible in such cases.

Instead of trying to set a flag when you crash, which will at best be unreliable, how about instead setting it when you start and then unsetting it when you exit normally? That way it'll be left set if you haven't cleaned things up and you'll know next time you start.

walkytalky
  • 9,453
  • 2
  • 36
  • 44
  • thx for the idea. This is exactly what i need. I think JeremyP was a tiny bit earlier with his answer so i marked his as the correct one. But i upvoted yours too. And thx for the fast reply. :) – Maverick1st Jun 07 '11 at 15:06
0

One thought... wrap your main starting code in your app delegate in a @Try @Catch block to handle the exception causing the crash. Perform any last minute code there (like setting your flag).

Richard Brightwell
  • 3,012
  • 2
  • 20
  • 22