iOS:当应用程序因任何崩溃而退出时,是否有任何委托方法?
当我的应用程序在崩溃时因内存不足,内存泄漏等一般崩溃而退出时,我想与我的服务器进行一些交互。 我想知道,是否有任何委托方法将在这种情况下调用,以便我可以快速联系我的服务器,并立即在应用程序退出之前,由于任何崩溃。
谢谢。
正如你所解释的,你需要亲密的服务器,你可以立即联系你的服务器之前,应用程序因任何崩溃退出。
在这种情况下,你应该设置exception handler
因为会发生任何exception
你会得到通知
看你怎么做到这一点
在applicationDidFixnishLaunchin
Appdelegate
类的方法中写这个NSSetUncaughtExceptionHandler (&uncaughtExceptionHandler)
代码行
-(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"]; }
添加您自己的exception处理程序,以捕获错误。
首先定义exception方法:
void uncaughtExceptionHandler(NSException *exception) { // You code here, you app will already be unload so you can only see what went wrong. }
然后告诉应用程序使用你的exception处理程序:
- (void)applicationDidFinishLaunching:(UIApplication *)application { NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); // The rest if you code .... }
希望它可以帮助你。