- 浏览: 25290 次
- 性别:
- 来自: 北京
最新评论
-
micallz:
绝知此事要躬行。
Git忽略文件(转载好文) -
lalakang:
提示找不到新项目的文件啊
ios如何在当前工程中添加编辑新建的FramesWork (两个ios工程联调同样适用 ) -
lalakang:
表示搞不对啊!!!
ios如何在当前工程中添加编辑新建的FramesWork (两个ios工程联调同样适用 )
转自:http://blog.chukong-inc.com/index.php/2012/08/24/如何在ios应用中捕捉异常/
虽然大家都不愿意看到程序崩溃,但可能崩溃是每个应用必须面对的现实,既然崩溃已经发生,无法阻挡了,那我们就让它崩也崩得淡定点吧。
IOS SDK中提供了一个现成的函数 NSSetUncaughtExceptionHandler 用来做异常处理,但功能非常有限,而引起崩溃的大多数原因如:内存访问错误,重复释放等错误就无能为力了,因为这种错误它抛出的是Signal,所以必须要专门做Signal处理。步骤如下:
首先定义一个UncaughtExceptionHandler类,.h头文件的代码如下:
#import <UIKit/UIKit.h>
@interface UncaughtExceptionHandler : NSObject
{
BOOL dismissed;
}
@end
void InstallUncaughtExceptionHandler();
然后在.mm文件实现InstallUncaughtExceptionHandler(),如下:
void InstallUncaughtExceptionHandler()
{
signal(SIGABRT, MySignalHandler);
signal(SIGILL, MySignalHandler);
signal(SIGSEGV, MySignalHandler);
signal(SIGFPE, MySignalHandler);
signal(SIGBUS, MySignalHandler);
signal(SIGPIPE, MySignalHandler);
}
这样,当应用发生错误而产生上述Signal后,就将会进入我们自定义的回调函数MySignalHandler。为了得到崩溃时的现场信息,还可以加入一些获取CallTrace及设备信息的代码,.mm文件的完整代码如下:
#import “UncaughtExceptionHandler.h”
#include <libkern/OSAtomic.h>
#include <execinfo.h>
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);
int i;
NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];
for (
i = UncaughtExceptionHandlerSkipAddressCount;
i < UncaughtExceptionHandlerSkipAddressCount +
UncaughtExceptionHandlerReportAddressCount;
i++)
{
[backtrace addObject:[NSString stringWithUTF8String:strs[i]]];
}
free(strs);
return backtrace;
}
- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex
{
if (anIndex == 0)
{
dismissed = YES;
}
}
- (void)handleException:(NSException *)exception
{
UIAlertView *alert =
[[[UIAlertView alloc]
initWithTitle:NSLocalizedString(@”Unhandled exception”, nil)
message:[NSString stringWithFormat:NSLocalizedString(
@"You can try to continue but the application may be unstable.\n"
@"%@\n%@", nil),
[exception reason],
[[exception userInfo] objectForKey:UncaughtExceptionHandlerAddressesKey]]
delegate:self
cancelButtonTitle:NSLocalizedString(@”Quit”, nil)
otherButtonTitles:NSLocalizedString(@”Continue”, 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
NSString* getAppInfo()
{
NSString *appInfo = [NSString stringWithFormat:@"App : %@ %@(%@)\nDevice : %@\nOS Version : %@ %@\nUDID : %@\n",
[[NSBundle mainBundle] objectForInfoDictionaryKey:@”CFBundleDisplayName”],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@”CFBundleShortVersionString”],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@”CFBundleVersion”],
[UIDevice currentDevice].model,
[UIDevice currentDevice].systemName,
[UIDevice currentDevice].systemVersion,
[UIDevice currentDevice].uniqueIdentifier];
NSLog(@”Crash!!!! %@”, appInfo);
return appInfo;
}
void MySignalHandler(int signal)
{
int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount);
if (exceptionCount > UncaughtExceptionMaximum)
{
return;
}
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.\n"
@"%@", nil),
signal, getAppInfo()]
userInfo:
[NSDictionary
dictionaryWithObject:[NSNumber numberWithInt:signal]
forKey:UncaughtExceptionHandlerSignalKey]]
waitUntilDone:YES];
}
void InstallUncaughtExceptionHandler()
{
signal(SIGABRT, MySignalHandler);
signal(SIGILL, MySignalHandler);
signal(SIGSEGV, MySignalHandler);
signal(SIGFPE, MySignalHandler);
signal(SIGBUS, MySignalHandler);
signal(SIGPIPE, MySignalHandler);
}
在应用自身的 didFinishLaunchingWithOptions 前,加入一个函数:
- (void)installUncaughtExceptionHandler
{
InstallUncaughtExceptionHandler();
}
最后,在 didFinishLaunchingWithOptions 中加入这一句代码就行了:
[self InstallUncaughtExceptionHandler];
现在,基本上所有崩溃都能Hold住了。崩溃时将会显示出如下的对话框:
这样在崩溃时还能从容地弹出对话框,比起闪退来,用户也不会觉得那么不爽。然后在下次启动时还可以通过邮件来发送Crash文件到邮箱,这就看各个应用的需求了。
发表评论
-
xcode4.3下制作framework
2013-04-09 09:25 1929转自:xcode4.3下制作framework——(xc ... -
对比iOS中的四种数据存储
2013-04-07 16:13 995摘自:infoq http://www.inf ... -
iOS网络组件对比:AFNetworking VS ASIHTTPRequest
2013-03-07 10:10 1457在开发iOS应用过程中, ... -
ios如何在当前工程中添加编辑新建的FramesWork (两个ios工程联调同样适用 )
2012-12-12 14:57 3096唐彬琪 可能很多大牛都见过FaceBook的three2 ... -
Xcode4快速Doxygen文档注释 — 简明图文教程
2012-12-03 17:28 1143转自:http://blog.chukong-inc.com/ ... -
Git忽略文件(转载好文)
2012-11-21 11:29 1212摘自:http://cwind.iteye.com/bl ... -
iOS开发细节记录汇总
2012-11-13 14:56 10451. 模拟器网速调整: 限制时输入下面两行,根据需要修 ... -
iOS签名总结
2012-11-13 11:34 1903摘自:http://www.devdiv.com/iOS签名总 ... -
iPhone开发资源汇总
2012-11-13 14:50 2073很多ios效果,以及控件集合 http://code4app. ... -
模拟器与真机下ffmpeg的编译方法(总结版)
2012-11-13 14:51 1370模拟器与真机下ffmpeg的编译方法(总结版) 编译f ... -
开源的电子书阅读器项目
2012-11-07 14:57 1823iphone平台上开源的电子书阅读器项目: https ... -
iOS高效开发必备的10款Objective-C类库
2012-11-07 14:28 689这里列出的库等功能使开发更容易。如果你是一个iOS开发人员,在 ... -
iOS6.0框架及功能更新小结
2012-11-07 14:58 750iOS6.0框架及功能更新小结 1. Maps 替换 ... -
const、static的理解
2012-11-07 14:58 1179对于C/C++语言来讲, const就是只读的意思,只在声明中 ... -
ios控件自定义指引
2012-11-07 14:58 1055转自:http://leakswww.devdiv ...
相关推荐
用HTML5开发iOS应用
iOS应用逆向与安全.pdf
dump、Theos、Cycript、IDA、GDB等最常用逆向工具的使用方法,进而分析使用这些工具时的思考方式和碰到问题时的解决思路,最后以社交应用消息拦截、iOS电话相关操作等4个极具代表性的实例总结iOS应用逆向工程在实战...
最新版的用swift4 开发ios应用,好东东,开发必备学习 原版 Beginning iPhone Development with Swift 4: Exploring the iOS SDK
高仿微信,iOS应用开发模板.zip ios 开发模板。高仿微信,iOS应用开发模板.zip ios 开发模板。高仿微信,iOS应用开发模板.zip ios 开发模板。高仿微信,iOS应用开发模板.zip ios 开发模板。高仿微信,iOS应用开发...
简单版的防崩溃,防止数组越界,子线程刷线UI等崩溃,
ios应用源码之分类 .
IOS应用源码——应用程序密码锁.zip
ios应用的升级更新评分提示
ios应用开发入门指南,适合初学者熟悉ios系统,熟悉基于apple的app使用开发方法。
iOS应用逆向工程 第2版_完整版 PDF电子书下载 带书签目录
DSYM--iOS崩溃日志分析工具 根据崩溃产生的内存地址,跟踪崩溃位置
ios应用源码之二维码生成器 .
《IOS应用安全攻防》PDF高清电子书,不过是英文的。
ios应用商店审核指南中文版,审核相关,审核问题
简单的ios应用开发入门指南,适合初学者看
高性能 iOS 应用开发 中文完整版 大家一起学习 iOS 吧
ios应用源码之动画 .
触觉反馈及蓝牙低功耗和iOS应用程序.zip触觉反馈及蓝牙低功耗和iOS应用程序.zip触觉反馈及蓝牙低功耗和iOS应用程序.zip触觉反馈及蓝牙低功耗和iOS应用程序.zip触觉反馈及蓝牙低功耗和iOS应用程序.zip触觉反馈及蓝牙...