iOS:当应用程序因崩溃而退出时是否有任何委托方法

Ste*_*lla 6 ios

当我的应用程序在崩溃时因内存不足,内存泄漏等问题退出时,我想与我的服务器进行一些交互.一般崩溃.我想知道,在这种情况下是否会调用任何委托方法,以便我可以在应用程序因崩溃而退出之前立即联系我的服务器.

谢谢.

Kam*_*had 8

正如您所解释的那样,您需要亲密的服务器,您可以在应用程序因崩溃而退出之前立即联系您的服务器.

在这种情况下,你应该设置exception handler任何exception将发生你将获得通知

看看你怎么做到这一点

NSSetUncaughtExceptionHandler (&uncaughtExceptionHandler)applicationDidFixnishLaunchinMethod of AppdelegateClass中编写这行代码

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

  }
Run Code Online (Sandbox Code Playgroud)