尽管大家都不愿意看到程序崩溃,但可能崩溃是每一个应用必须面对的现实。既然崩溃已经发生。无法阻挡了。那我们就让它崩也崩得淡定点吧。

IOS SDK中提供了一个现成的函数 NSSetUncaughtExceptionHandler 用来做异常处理。但功能很有限。而引起崩溃的大多数原因如:内存訪问错误。反复释放等错误就无能为力了,由于这样的错误它抛出的是Signal。所以必需要专门做Signal处理。

首先定义一个UncaughtExceptionHandler类。代码例如以下:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface UncaughtExceptionHandler : NSObject
{
BOOL dismissed;
}
+(void) InstallUncaughtExceptionHandler;
@end
//利用 NSSetUncaughtExceptionHandler,当程序异常退出的时候,能够先进行处理。然后做一些自己定义的动作,比方以下一段代码,就是网上有人写的,直接在发生异常时给某人发送邮件。</span>
void UncaughtExceptionHandlers (NSException *exception);

#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;
NSString* getAppInfo()
{
NSString *appInfo = [NSString stringWithFormat:@"App : %@ %@(%@)\nDevice : %@\nOS Version : %@ %@\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;
}
if(signal==11)
{//比較坑爹的是 我遇到的一个问题仅仅有iPhone5出现故障 可是我这边測试的没有iPhone5 无法直接log 可能是内存不足 果然 删除几个应用就能够了 所以加了这句
        UIAlertView * tip2 = [[UIAlertView alloc]initWithTitle:@"可能原因:key" message:@"内存不足" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[tip2 show];
[tip2 release];
} 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]; } @implementation UncaughtExceptionHandler
+(void) InstallUncaughtExceptionHandler
{
signal(SIGABRT, MySignalHandler);
signal(SIGILL, MySignalHandler);
signal(SIGSEGV, MySignalHandler);
signal(SIGFPE, MySignalHandler);
signal(SIGBUS, MySignalHandler);
signal(SIGPIPE, MySignalHandler);
}
+ (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];
}
}
void UncaughtExceptionHandlers (NSException *exception) {
NSArray *arr = [exception callStackSymbols];
NSString *reason = [exception reason];
NSString *name = [exception name];
NSString *urlStr = [NSString stringWithFormat:@"mailto://1140454645@qq.com? subject=bug报告&body=感谢您的配合!<br><br><br>"
"错误详情:<br>%@<br>--------------------------<br>%@<br>---------------------<br>%@",
name,reason,[arr componentsJoinedByString:@"<br>"]];
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url]; //或者直接用代码,输入这个崩溃信息,以便在console中进一步分析错误原因
NSLog(@"1heqin, CRASH: %@", exception);
NSLog(@"heqin, Stack Trace: %@", [exception callStackSymbols]);
} @end

然后在delegate文件中面- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函数里面

 [UncaughtExceptionHandler InstallUncaughtExceptionHandler];
NSSetUncaughtExceptionHandler (&UncaughtExceptionHandlers);

【IOS】异常捕获 拒绝闪退 让应用从容的崩溃 UncaughtExceptionHandler的更多相关文章

  1. 异常捕获拒绝闪退 让应用从容的崩溃UncaughtExceptionHandler

    虽然大家都不愿意看到程序崩溃,但可能崩溃是每个应用必须面对的现实,既然崩溃已经发生,无法阻挡了,那我们就让它崩也崩得淡定点吧. IOS SDK中提供了一个现成的函数 NSSetUncaughtExce ...

  2. Unity3D 游戏在 iOS 上因为 trampolines 闪退的原因与解决办法

    崩溃的情况 进入游戏一会儿,神马都不要做,双手离开手机,盯着屏幕看吧,游戏会定时从服务器那儿读取一些数据,时间一长,闪退了.尼玛问题是神马呢?完全没有头绪,不过大体猜测是因为网络请求导致的,那么好,先 ...

  3. Unity3D游戏在iOS上因为trampolines闪退的原因与解决办法

    http://7dot9.com/?p=444 http://whydoidoit.com/2012/08/20/unity-serializer-mono-and-trampolines/ 确定具体 ...

  4. iOS异常捕获

    文章目录 一. 系统Crash 二. 处理signal 下面是一些信号说明 关键点注意 三. 实战 四. Crash Callstack分析 – 进⼀一步分析 五. demo地址 六. 参考文献 前言 ...

  5. 分割视图控制器(UISplitViewController) 改_masterColumnWidth 导致在 IOS 10中出现闪退

    默认UISplitViewController的Master和Detail的宽度是固定的,可以通过下面的方式来改变 [splitViewController setValue:[NSNumber nu ...

  6. #iOS问题记录#WKWebView 闪退异常

    异常描述: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug 问题描述 ...

  7. ios 异常捕获

    @try { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate c ...

  8. iOS中app启动闪退的原因

    这种情况应和所谓的内存不足关系不大,很少有程序会在初始化时载入大量内容导致崩溃,并且这类问题也很容易在开发阶段被发现,所以内存不足造成秒退的可能性低(内存不足退,通常是程序用了一段时间,切换了几个画面 ...

  9. iOS异常捕获和处理

    2013年4月份整理的代码,仅作记录:   //先宏定义 //发布和未发布状态的日志切换 #ifdef DEBUG     //异常栈开关     #define STACK_KEY YES     ...

随机推荐

  1. WPF 禁用中文

    <TextBox InputMethod.IsInputMethodEnabled="False" />

  2. 警告: [SetPropertiesRule]{Context/Loader} Setting property 'useSystemClassLoaderAsParent' to 'false' did not find a matching property.

    警告: [SetPropertiesRule]{Context/Loader} Setting property 'useSystemClassLoaderAsParent' to 'false' d ...

  3. 统一建模语言简介UML

    统一建模语言(Unified Modeling Language,UML)是用来设计软件蓝图的可视化建模语言,1997 年被国际对象管理组织(OMG)采纳为面向对象的建模语言的国际标准.它的特点是简单 ...

  4. 2019-10-16-WPF-控件-Content-的内容不显示下划线字符串

    title author date CreateTime categories WPF 控件 Content 的内容不显示下划线字符串 lindexi 2019-10-16 09:21:32 +080 ...

  5. scikit-learn的基本使用

    在机器学习和数据挖掘的应用中,scikit-learn是一个功能强大的python包.在数据量不是过大的情况下,可以解决大部分问题.学习使用scikit-learn的过程中,我自己也在补充着机器学习和 ...

  6. C#解析字符串公式

    /// <summary> /// 中缀表达式到逆波兰表达式的转换及求值 /// </summary> public class RpnExpression { #region ...

  7. Thinkphp 数据库配置参数

    mysql配置 'DB_USER' => array( 'DB_TYPE' => 'mysql', 'DB_HOST' => '127.0.0.1', 'DB_NAME' => ...

  8. Jan's light oj 01--二分搜索篇

    碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...

  9. 常见任务&基本工具 1 软件包管理

    打包系统主要有两个阵营 包文件的简介 Package files are created by a person known as a package maintainer, often (but n ...

  10. oracle习题-emp表查询练习

    emp表查询练习 1 查询emp表的全部记录 Select * from emp; 2 查询出每个雇员的编号.姓名.基本工资 Select empno,ename,sal from emp; 3 查询 ...