6

I would like to do some interaction with my server when my app quits at the time of crash due to low memory, memory leaks etc. general crashes. I would like to know, Is there any delegate methods will be called in this scenario, so that i can contact my server quickly and immediately before the app quits due to any crash.

Thank you.

Stella
  • 1,728
  • 5
  • 41
  • 95
  • 1
    I dont think there is any delegate method for logging crash. This post is somewhat similar to yours. http://stackoverflow.com/questions/8233388/ios-crash-log-catch-debug-info-catch-and-send-via-email-to-the-dev-team – Deepesh Gairola May 02 '13 at 04:41
  • - (void)didReceiveMemoryWarning method get called if low memory – Sugan S May 02 '13 at 05:15
  • Don't reinvent the wheel: if you want to log crash info/stack trace etc, I would highly recommend [Crashlytics](http://crashlytics.com) - its free and requires zero effort from your side. You can find more info and other solutions in this [excellent answer](http://stackoverflow.com/a/8242215/440060). – maroux May 02 '13 at 07:17

2 Answers2

8

As you explained you need to intimate server you can contact your server immediately before the app quits due to any crash.

in that case you should set exception handler as any exception will occur you will get notify

See how can you do this

write this NSSetUncaughtExceptionHandler (&uncaughtExceptionHandler) line of code in applicationDidFixnishLaunchin Method of Appdelegate Class

 -(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:  
  (NSDictionary*)launchOptions
 {   
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); 

   EDIT:
   if( [[NSUserDefaults standardUserDefaults] boolForKey:@"isExceptionOccured"])
   {
    //call sever code here
    [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"isExceptionOccured"];
   }
   //rest of your code
 }


 void uncaughtExceptionHandler(NSException *exception)
  {


  NSLog(@"Exception Got %@",[exception description]);
  //do what ever you what here 
  //can save any `bool` so that as aaplication  run on immediate next launching of crash
  //could intimate any thing

  EDIT: 

  [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isExceptionOccured"];

  }
Kamar Shad
  • 6,089
  • 1
  • 29
  • 56
  • Hi, This is for logging exception handler, but i need to send to my server just before the crash (only) quits my app. So, in this case where i can add code to upload the crash log? – Stella May 02 '13 at 06:20
  • @user213199 you can create server connection in that handler method or can keep a bool and use in next immediate launching of app. – Kamar Shad May 02 '13 at 06:23
  • So, do you meant to say we can use this place to upload crash logs to our server even when the app is going to quit? But as per Apple, we can only to do such things in applicationWillTerminate within 5 secs interval? – Stella May 02 '13 at 06:26
  • i did not mean so, not saying upload any crash log to server.tell me what you want by interacting to server as any crash happens. – Kamar Shad May 02 '13 at 06:32
  • i want to upload any kind of crash logs to my server to know the reason for crash, information etc. so looking for a place where i can add code to upload such logs just before the crash quitting my app. – Stella May 02 '13 at 06:35
  • u can send that `[exception description]` to server from inside the `uncaughtExceptionHandler` – Kamar Shad May 02 '13 at 06:38
  • I can send it just before the app quits(applicationWillTerminate) due to any crash? There are two important points in my requirement. 1.) I want to log any crash errors. But, UncaughtExceptionHandler logs only uncaught exceptions, not bad_access, bad release etc. crash errors. 2.) Is it 100% sure that, Will it log and send to server just before applicationWillTerminate being called when quitting the app? – Stella May 02 '13 at 06:44
  • @user213199 i have tested and as far as i know , it it will fire for bad release , bad_access exception also. – Kamar Shad May 02 '13 at 06:47
  • Are you able to upload such crash logs using like this approach, to your server "during crash and just before the app quits" combination without any issues? When i see API ref. NSSetUncaughtExceptionHandler, it is mentioned in Apple site its for 10.x Mac supported API. Does this work on iOS as well? – Stella May 02 '13 at 07:27
  • No, I DON'T WANT to send logs next time app launch intimation at all. I want to send logs immediately the crash happens and before quitting the app, that's what i mentioned above many times. – Stella May 02 '13 at 07:32
  • @user213199 it works for Mac OS x and iOS both.if you don't want to send log reports next time launching do comments on `Edit section` and send the Crash log in exception handler.apart from this i would suggest you here we can just give you the logic and way of doing the things we can't provide you running/tested code.this would be done by you. – Kamar Shad May 02 '13 at 08:09
  • No. my simple question is, i want to send/share crash log to my server at the time of crash, will this method -(void) uncaughtExceptionHandler(NSException *exception) is the right place to do? Can i use this place to send to my server before app quits to device home screen? I CAN do rest everything else what i want. That's all i want. – Stella May 02 '13 at 09:36
  • Be sure to synchronize the NSUserDefaults to persist the value during crash [[NSUserDefaults standardUserDefaults]synchronize] – Durai Amuthan.H Nov 30 '13 at 07:40
3

Add your own exception handler, to catch the error.

Firstly define the exception method:

void uncaughtExceptionHandler(NSException *exception) {
    // You code here, you app will already be unload so you can only see what went wrong.
}

Then tell the app to use your exception handler:

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
    // The rest if you code  ....
}

hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • But there is no way to distinguish between a simple home button press and a crash – Scott Berrevoets May 02 '13 at 05:14
  • Hi, This is for logging exception handler, but i need to send to my server just before the crash (only) quits my app. So, in this case where i can add code to upload the crash log? – Stella May 02 '13 at 06:21
  • You can call your web service in this handler. – Nishant Tyagi May 02 '13 at 06:27
  • NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [prefs setObject:@"Exception details" forKey:@"LastException"]; the above code snippet works normally but when i placed inside the uncaughtexceptionhandler it doesn't save the given exception details. – Durai Amuthan.H Nov 30 '13 at 07:08
  • I found the answer myself.Adding [[NSUserDefaults standardUserDefaults]synchronize]; with the existing code snippet solved my problem. – Durai Amuthan.H Nov 30 '13 at 07:18
  • Where do you put the [[NSUserDefaults standardUserDefaults]synchronize]? I'm putting it inside uncaughtExceptionHandler but it doesn't save the value in nsuserdefaults? Please help – CarlosAndres Jun 12 '17 at 18:14