IOS: Is there any delegate method when the application closes due to some kind of failure

I would like to do some interaction with my server when my application crashes during a crash due to low memory, memory leaks, etc. general failures. I would like to know if there are any delegate methods in this case so that I can quickly and quickly contact my server before the application shuts down due to some kind of failure.

Thanks.

+6
source share
2 answers

As you explained, you need to install an intimate server, you can contact your server immediately before the application terminates due to a failure.

in this case you should set an exception handler , as soon as an exception occurs, you will receive a notification

See how you can do it.

write this line of NSSetUncaughtExceptionHandler (&uncaughtExceptionHandler) code in applicationDidFixnishLaunchin Appdelegate Method 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"]; } 
+6
source

Add your own exception handler to catch the error.

First, define an 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 application to use the exception handler:

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

Hope this helps you.

+3
source

Source: https://habr.com/ru/post/944093/


All Articles