iOS:由于崩溃而退出应用程序时,是否有任何委托方法 [英] 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.
谢谢.
推荐答案
正如您所解释的,您需要封闭服务器,可以在应用由于任何崩溃而退出之前立即与服务器联系.
As you explained you need to intimate server you can contact your server immediately before the app quits due to any crash.
在这种情况下,您应该设置exception handler
,因为任何exception
都会发生,您将得到通知
in that case you should set exception handler
as any exception
will occur you will get notify
看看你怎么做
在applicationDidFixnishLaunchin
类的方法applicationDidFixnishLaunchin
中写这行代码NSSetUncaughtExceptionHandler (&uncaughtExceptionHandler)
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"];
}
这篇关于iOS:由于崩溃而退出应用程序时,是否有任何委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!