4

I am trying to create an iOS crash reporter tool. I wonder if the application can send crash info after it was terminated.

So my questions are: - What is a lifecycle of iOS application after termination? - Where can I read more about what iOS does to application on termination?

mafonya
  • 2,152
  • 22
  • 21
  • So, my question refers to what happens after that lifecycle. – mafonya Oct 21 '13 at 11:55
  • You cannot do anything after an app terminates. You can only do something in the [applicationWillTerminate](https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/applicationWillTerminate:) method. – Black Frog Oct 21 '13 at 11:57
  • Maybe you should investigate how the tools like "crashlytics" or "google analytics" are working. I would use a background service for sending the detailed crash info, and send a short report to that background service in the applicationWillTerminate method. – Yunus Nedim Mehel Oct 21 '13 at 12:01
  • Thanks, Yunus. Unfortunately you can't run background service on iOS. – mafonya Oct 21 '13 at 12:05
  • 1
    This isn't really an answer but maybe look at how `testflight` implements the crash reported in their SDK https://testflightapp.com/sdk/download/ They call exceptions based on whether the app has the following signal states `SIGILL`, `SIGABRT` and `SIGBUS` maybe you could so something simpler. – Popeye Oct 21 '13 at 12:24

4 Answers4

7

Doing any non async-safe task when the app crashed is highly NOT-recommendable!

  1. You are not allowed to allocate any new memory at that time
  2. You are only allowed to use async safe code (Any Objective-C code is NOT async safe)
  3. You need to take into account that memory is already corrupted
  4. You need to implement async-safe networking code
  5. and many many more reasons.

See these blog posts from Landon Fuller, the author of PLCrashReporter:

  1. http://landonf.bikemonkey.org/code/crashreporting/Reliable_Crash_Reporting_1.1.20130119.html
  2. http://landonf.bikemonkey.org/code/objc/Reliable_Crash_Reporting.20110912.html

You are trying to solve a problem, that is not a problem in the real world. People do restart their apps and will send the crash reports.

Kerni
  • 15,241
  • 5
  • 36
  • 57
3

Yep, a sort of... you can handle exceptions, before iOS kills the crashing app, but you can't do any async operation (probably not totally true you can use background operation with expiration handler, or in iOS7 NSURLSession), such as sending the a file to a server, but you can do at the next restart.
The idea behind that is in -applicationDidFinishLaunching to set an exception handler:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        NSSetUncaughtExceptionHandler(&myExcHandler);
        /* your code*/
    }

myExcHandler is a C callback that accept an NSException as parameters that it will be called when an exception occurs.

void myExcHandler(NSException *exception)
{
  //set something on NSUserDefault to check at next start
}

It must be said that there are plenty of crashing report lib available. Do not reinvent the wheel ;-)

Andrea
  • 26,120
  • 10
  • 85
  • 131
  • You are right, I am trying not to invent another wheel. But all these existing solutions can't send the report immediately, so I am trying to find a way to do so. – mafonya Oct 21 '13 at 12:23
  • Probably is because it is not possible. Try with the expiration handler, but I'm not totally sure that is handled with the app killed, same for NSURLSession. – Andrea Oct 21 '13 at 12:27
0

From Apple docs

If your app is running (either in the foreground or background) at termination time, the system calls your app delegate’s applicationWillTerminate: method so that you can perform any required cleanup. You can use this method to save user data or app state information that you would use to restore your app to its current state on a subsequent launch. Your method has approximately 5 seconds to perform any tasks and return. If it does not return in time, the app is killed and removed from memory.

Scroll down to "App Termination" to read more information about this.

Hope it is of any help

Amar
  • 13,202
  • 7
  • 53
  • 71
Stian Berg Larsen
  • 543
  • 2
  • 10
  • 29
  • 2
    This is not true when the users kills the app, or the app gets purshed from memory in low memory situations. – rckoenes Oct 21 '13 at 11:53
0

The last event you got for application terminate is in applicationWillTerminate method . This method will not called if application is suspended . You can monitor crash log on your machine of application if your application crashed after termination here :

~/Library/Logs/CrashReporter/MobileDevice/<your iPhone’s name>/

V-Xtreme
  • 7,230
  • 9
  • 39
  • 79