在广东中山小榄镇小榄买的卡可以在佛山使用流量日租包吗?

IOS自动捕获程序崩溃日志再发送邮件提示开发者-ios8-手机开发-壹聚教程网IOS自动捕获程序崩溃日志再发送邮件提示开发者
运行app崩溃是常有的问题,作为开发者应该尽量解决这些bug,本文来介绍几个ios自动捕获程序崩溃日志,然后再将崩溃日志以邮件的形式发送给开发者,这样可以即时的修改bug。
我们常常会遇到iPhone手机或者iPad平板上运行APP崩溃的问题,有时候打开某个APP,却一下子“闪退”了。对于开发者来说,这个绝对是头疼的问题。那么如何获取到iOS设备崩溃日志呢?这个提供一些简单的方法,共开发者与用户沟通使用。  iOS开发中遇到程序崩溃是很正常的事情,如何在程序崩溃时捕获到异常信息并通知开发者?  下面就介绍如何在iOS中实现:  1. 在程序启动时加上一个异常捕获监听,用来处理程序崩溃时的回调动作  代码如下:  NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);  官方文档介绍:Sets the top-level error-handling function where you can perform last-minute logging before the program terminates.  UncaughtExceptionHandler是一个函数指针,该函数需要我们实现,可以取自己想要的名字。当程序发生异常崩溃时,该函数会得到调用,这跟C,C++中的回调函数的概念是一样的。  2. 实现自己的处理函数  代码如下:  void UncaughtExceptionHandler(NSException *exception) {  NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息  NSString *reason = [exception reason];//非常重要,就是崩溃的原因  NSString *name = [exception name];//异常类型  NSLog(@&exception type : %@ n crash reason : %@ n call stack info : %@&, name, reason, arr);  }  以上代码很简单,但是带来的作用是非常大的。iOS 中捕获程序崩溃日志再发送给开发者iOS开发中遇到程序崩溃是很正常的事情,如何在程序崩溃时捕获到异常信息并通知开发者,是大多数软件都选择的方法。下面就介绍如何在iOS中实现:1. 在程序启动时加上一个异常捕获监听,用来处理程序崩溃时的回调动作& NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);& 官方文档介绍:Sets the top-level error-handling function where you can perform last-minute logging before the program terminates.& UncaughtExceptionHandler是一个函数指针,该函数需要我们实现,可以取自己想要的名字。当程序发生异常崩溃时,该函数会得到调用,这跟C,C++中的回调函数的概念是一样的。2. 实现自己的处理函数void UncaughtExceptionHandler(NSException *exception) {&&& NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息&&& NSString *reason = [exception reason];//非常重要,就是崩溃的原因&&& NSString *name = [exception name];//异常类型& &&&& NSLog(@&exception type : %@ \n crash reason : %@ \n call stack info : %@&, name, reason, arr);}以上代码很简单,但是带来的作用是非常大的。获取到了崩溃的日子,如何发送给开发者呢,目前一般有以下两种方式:1. 将崩溃信息持久化在本地,下次程序启动时,将崩溃信息作为日志发送给开发者。2. 通过邮件发送给开发者。 不过此种方式需要得到用户的许可,因为iOS不能后台发送短信或者邮件,会弹出发送邮件的界面,只有用户点击了发送才可发送。 不过,此种方式最符合苹果的以用户至上的原则。发送邮件代码也很简单:&NSString *crashLogInfo = [NSString stringWithFormat:@&exception type : %@ \n crash reason : %@ \n call stack info : %@&, name, reason, arr];&&& NSString *urlStr = [NSString stringWithFormat:@&mailto://?subject=bug报告&body=感谢您的配合!&&&&&&&&&&&&&&&&&&&&&&&& &错误详情:%@&,&&&&&&&&&&&&&&&&&&&&&&& crashLogInfo];&&& NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];&&& [[UIApplication sharedApplication] openURL:url];以上就是iOS中捕获异常常用的方法,大家可以不妨一试!IOS程序异常crash捕获与拦截开发iOS应用,解决Crash问题始终是一个难题。Crash分为两种,一种是由EXC_BAD_ACCESS引起的,原因是访问了不属于本进程的内存地址,有可能是访问已被释放的内存;另一种是未被捕获的Objective-C异常(NSException),导致程序向自身发送了SIGABRT信号而崩溃。其实对于未捕获的Objective-C异常,我们是有办法将它记录下来的,如果日志记录得当,能够解决绝大部分崩溃的问题。这里对于UI线程与后台线程分别说明。一:在.h文件中编写@interface UncaughtExceptionHandler : NSObject{ &&&& BOOL &} && @end &void HandleException(NSException *exception); &void SignalHandler(int signal); && & void InstallUncaughtExceptionHandler(void); &//该代码片段来自于: /codes/objectc/5882二:在.m文件编写#import &UncaughtExceptionHandler.h& &# &libkern/OSAtomic.h& &#include &execinfo.h& && ///newbie/tutorial/72.html &NSString * const UncaughtExceptionHandlerSignalExceptionName = @&UncaughtExceptionHandlerSignalExceptionName&; &NSString * const UncaughtExceptionHandlerSignalKey = @&UncaughtExceptionHandlerSignalKey&; &NSString * const UncaughtExceptionHandlerAddressesKey = @&UncaughtExceptionHandlerAddressesKey&; && volatile int32_t UncaughtExceptionCount = 0; &const int32_t UncaughtExceptionMaximum = 10; && const NSInteger UncaughtExceptionHandlerSkipAddressCount = 4; &const NSInteger UncaughtExceptionHandlerReportAddressCount = 5; && @implementation UncaughtExceptionHandler && + (NSArray *)backtrace &{ &&&&& void* callstack[128]; &&&&& int frames = backtrace(callstack, 128); &&&&& char **strs = backtrace_symbols(callstack, frames); &&&&&& &&&&& &&&&& NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames]; &&&&& for ( &&&&&&&& i = UncaughtExceptionHandlerSkipAddressC &&&&&&&& i & UncaughtExceptionHandlerSkipAddressCount + &&&&&&&&&&&& UncaughtExceptionHandlerReportAddressC &&&&&&&& i++) &&&&& { &&&&&&&& [backtrace addObject:[NSString stringWithUTF8String:strs[i]]]; &&&&& } &&&&& free(strs); &&&&&& &&&&& &} && - (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex &{ &&&& if (anIndex == 0) &&&& { &&&&&&&& dismissed = YES; &&&& }else if (anIndex==1) { &&&&&&&& NSLog(@&ssssssss&); &&&& } &} && - (void)validateAndSaveCriticalApplicationData &{ &&&&& &} && - (void)handleException:(NSException *)exception &{ &&&& [self validateAndSaveCriticalApplicationData]; &&&&& &&&& UIAlertView *alert = &&&&&&&& [[[UIAlertView alloc] &&&&&&&&&&&& initWithTitle:NSLocalizedString(@&抱歉,程序出现了异常&, nil) &&&&&&&&&&&& message:[NSString stringWithFormat:NSLocalizedString( &&&&&&&&&&&&&&&& @&如果点击继续,程序有可能会出现其他的问题,建议您还是点击退出按钮并重新打开\n\n& &&&&&&&&&&&&&&&& @&异常原因如下:\n%@\n%@&, nil), &&&&&&&&&&&&&&&& [exception reason], &&&&&&&&&&&&&&&& [[exception userInfo] objectForKey:UncaughtExceptionHandlerAddressesKey]] &&&&&&&&&&&& delegate:self &&&&&&&&&&&& cancelButtonTitle:NSLocalizedString(@&退出&, nil) &&&&&&&&&&&& otherButtonTitles:NSLocalizedString(@&继续&, nil), nil] &&&&&&&& autorelease]; &&&& [alert show]; &&&&& &&&& CFRunLoopRef runLoop = CFRunLoopGetCurrent(); &&&& CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop); &&&&& &&&& while (!dismissed) &&&& { &&&&&&&& for (NSString *mode in (NSArray *)allModes) &&&&&&&& { &&&&&&&&&&&& CFRunLoopRunInMode((CFStringRef)mode, 0.001, false); &&&&&&&& } &&&& } &&&&& &&&& CFRelease(allModes); && &&& NSSetUncaughtExceptionHandler(NULL); &&&& signal(SIGABRT, SIG_DFL); &&&& signal(SIGILL, SIG_DFL); &&&& signal(SIGSEGV, SIG_DFL); &&&& signal(SIGFPE, SIG_DFL); &&&& signal(SIGBUS, SIG_DFL); &&&& signal(SIGPIPE, SIG_DFL); &&&&& &&&& if ([[exception name] isEqual:UncaughtExceptionHandlerSignalExceptionName]) &&&& { &&&&&&&& kill(getpid(), [[[exception userInfo] objectForKey:UncaughtExceptionHandlerSignalKey] intValue]); &&&& } &&&& else &&&& { &&&&&&&& [exception raise]; &&&& } &} && @end && void HandleException(NSException *exception) &{ &&&& int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount); &&&& if (exceptionCount & UncaughtExceptionMaximum) &&&& { &&&&&&&& &&&& } &&&&& &&&& NSArray *callStack = [UncaughtExceptionHandler backtrace]; &&&& NSMutableDictionary *userInfo = &&&&&&&& [NSMutableDictionary dictionaryWithDictionary:[exception userInfo]]; &&&& [userInfo &&&&&&&& setObject:callStack &&&&&&&& forKey:UncaughtExceptionHandlerAddressesKey]; &&&&& &&&& [[[[UncaughtExceptionHandler alloc] init] autorelease] &&&&&&&& performSelectorOnMainThread:@or(handleException:) &&&&&&&& withObject: &&&&&&&&&&&& [NSException &&&&&&&&&&&&&&&& exceptionWithName:[exception name] &&&&&&&&&&&&&&&& reason:[exception reason] &&&&&&&&&&&&&&&& userInfo:userInfo] &&&&&&&& waitUntilDone:YES]; &} && void SignalHandler(int signal) &{ &&&& int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount); &&&& if (exceptionCount & UncaughtExceptionMaximum) &&&& { &&&&&&&& &&&& } &&&&& &&&& NSMutableDictionary *userInfo = &&&&&&&& [NSMutableDictionary &&&&&&&&&&&& dictionaryWithObject:[NSNumber numberWithInt:signal] &&&&&&&&&&&& forKey:UncaughtExceptionHandlerSignalKey]; && &&& NSArray *callStack = [UncaughtExceptionHandler backtrace]; &&&& [userInfo &&&&&&&& setObject:callStack &&&&&&&& forKey:UncaughtExceptionHandlerAddressesKey]; &&&&& &&&& [[[[UncaughtExceptionHandler alloc] init] autorelease] &&&&&&&& performSelectorOnMainThread:@selector(handleException:) &&&&&&&& withObject: &&&&&&&&&&&& [NSException &&&&&&&&&&&&&&&& exceptionWithName:UncaughtExceptionHandlerSignalExceptionName &&&&&&&&&&&&&&&& reason: &&&&&&&&&&&&&&&&&&&& [NSString stringWithFormat: &&&&&&&&&&&&&&&&&&&&&&&& NSLocalizedString(@&Signal %d was raised.&, nil), &&&&&&&&&&&&&&&&&&&&&&&& signal] &&&&&&&&&&&&&&&& userInfo: &&&&&&&&&&&&&&&&&&&& [NSDictionary &&&&&&&&&&&&&&&&&&&&&&&& dictionaryWithObject:[NSNumber numberWithInt:signal] &&&&&&&&&&&&&&&&&&&&&&&& forKey:UncaughtExceptionHandlerSignalKey]] &&&&&&&& waitUntilDone:YES]; &} && void InstallUncaughtExceptionHandler(void) &{ &&&& NSSetUncaughtExceptionHandler(&HandleException); &&&& signal(SIGABRT, SignalHandler); &&&& signal(SIGILL, SignalHandler); &&&& signal(SIGSEGV, SignalHandler); &&&& signal(SIGFPE, SignalHandler); &&&& signal(SIGBUS, SignalHandler); &&&& signal(SIGPIPE, SignalHandler); &} &//该代码片段来自于: /codes/objectc/5882三:最后在AppDelegate中创建- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ && InstallUncaughtExceptionHandler(); &&&& self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; &&&& // Override point for customization after application launch. && self.viewController = [[[ViewController alloc] initWithNibName:@&ViewController& bundle:nil] autorelease]; && self.window.rootViewController = self.viewC &&&& [self.window makeKeyAndVisible]; &&&& return YES; &} &//该代码片段来自于: /codes/objectc/5882四:最后的测试- (IBAction)onclcko:(id)sender { &&& //& [alert show]; &&&& NSArray *arry=[NSArray arrayWithObject:@&sss&]; &&&& NSLog(@&%@&,[arry objectAtIndex:1]); &}
名称:大小:9.96MM下载:
上一页: &&&&&下一页:相关内容

我要回帖

更多关于 小榄到佛山沙堤机场 的文章

 

随机推荐