我希望在我的应用程序由于内存不足、内存泄漏等常见崩溃情况而崩溃时,能够与我的服务器进行一些交互。我想知道,在这种情况下是否会调用任何代理方法,以便在应用程序由于任何崩溃而退出之前,我可以快速且立即地与我的服务器联系。
谢谢。
谢谢。
如您所述,您需要通知服务器,在应用程序由于任何崩溃而退出之前可以立即联系您的服务器。
在这种情况下,您应该设置exception handler
,因为任何exception
发生时,您将收到通知。
以下是如何进行设置:
在Appdelegate
类的applicationDidFixnishLaunchin
方法中编写此行代码: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"];
}
uncaughtExceptionHandler
中从内部将 [exception description]
发送到服务器。 - Kamar Shad添加自己的异常处理程序,以捕获错误。
首先定义异常方法:
void uncaughtExceptionHandler(NSException *exception) {
// You code here, you app will already be unload so you can only see what went wrong.
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
// The rest if you code ....
}
希望这能对您有所帮助。