简体   繁体   中英

iOS: Is there any delegate method when the app quits due to any crash

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.

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"];

  }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM