首先介紹一款工具 定位解析信號異常上github上搜索DSYMTools,和郵件發(fā)送SKPSMTPMessage
配上?
1.解決普通和信號異常
直接使用先上代碼
.h文件
#import
#import
@interfaceUncaughtExceptionHandler :NSObject{
BOOLdismissed;
}
@end
voidHandleException(NSException*exception);
voidSignalHandler(intsignal);
voidInstallUncaughtExceptionHandler(void);
.m文件
#import"UncaughtExceptionHandler.h"
#include
#include
#import"SKPSMTPMessage.h"
#import"NSData+Base64Additions.h"
NSString*constUncaughtExceptionHandlerSignalExceptionName =@"UncaughtExceptionHandlerSignalExceptionName";
NSString*constUncaughtExceptionHandlerSignalKey =@"UncaughtExceptionHandlerSignalKey";
NSString*constUncaughtExceptionHandlerAddressesKey =@"UncaughtExceptionHandlerAddressesKey";
NSString*constUncaughtExceptionHandlerStackSysbolsArrayKey =@"UncaughtExceptionHandlerStackSysbolsArrayKey";
volatileint32_tUncaughtExceptionCount =0;
constint32_tUncaughtExceptionMaximum =20;
constNSIntegerUncaughtExceptionHandlerSkipAddressCount =0;
constNSIntegerUncaughtExceptionHandlerReportAddressCount =20;
@interfaceUncaughtExceptionHandler()
/**網(wǎng)絡(luò)請求管理者*/
@property(strong,nonatomic)YLTHTTPSessionManager*manager;
@end
@implementationUncaughtExceptionHandler
+ (NSArray*)backtrace {
void* callstack[128];
intframes =backtrace(callstack,128);
char**strs =backtrace_symbols(callstack, frames);
inti;
NSMutableArray*backtrace = [NSMutableArrayarrayWithCapacity:frames];
for(i =UncaughtExceptionHandlerSkipAddressCount; i
[backtraceaddObject:[NSStringstringWithUTF8String:strs[i]]];
}
free(strs);
returnbacktrace;
}
- (void)alertView:(UIAlertView*)anAlertView clickedButtonAtIndex:(NSInteger)anIndex {
if(anIndex ==0){
dismissed=YES;
}elseif(anIndex==1) {
NSLog(@"異常!7媳臁嘉冒!");
}
}
#pragma mark -上傳crash日志
- (void)validateAndSaveCriticalApplicationDataWithException:(NSException*)exception {
//發(fā)送crash到郵箱
[selfmessageEmailWithException:exception];
//發(fā)送crash到服務(wù)器
//[self messageServerithException:exception];
}
#pragma mark - <發(fā)送給后臺服務(wù)器------------------------------------------>
- (void)messageServerithException:(NSException*)exception {
//應(yīng)用版本
NSString*version =AppVersion;
if(nil== version) {
version =@"";
}
//設(shè)備版本
NSString*deviceModel = [PublicMethodiphoneType];
//系統(tǒng)版本
NSString*sysVersion = [UIDevicecurrentDevice].systemVersion;
//郵件主題
NSString*subject = [NSStringstringWithFormat:@"[Crash][iOS][%@][%@][%@]", version, sysVersion, deviceModel];
//調(diào)用棧
NSArray*stackSysbolsArray = [[exceptionuserInfo]objectForKey:UncaughtExceptionHandlerAddressesKey];
//崩潰原因
NSString*reason = [exceptionreason];
//崩潰原因
NSString*name = [exceptionname];
NSString*encryptDate = [PublicMethodencryptAESDataWithJSON];
NSMutableDictionary*dict = [NSMutableDictionarydictionary];
//上傳服務(wù)器接口
[self.managerPOST:@"https://passport.zgylt.com/Data/CollectionException"parameters:dictprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {
if([responseObject[@"Code"]isEqualToString:@"200"]) {
NSLog(@"發(fā)送成功");
}else{
NSLog(@"發(fā)送失敗");
}
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
NSLog(@"發(fā)送失敗%@",error);
}];
}
/**網(wǎng)絡(luò)請求管理者*/
- (YLTHTTPSessionManager*)manager {
if(!_manager) {
_manager= [YLTHTTPSessionManagermanager];
}
return_manager;
}
#pragma mark - <發(fā)送郵件------------------------------------------>
- (void)messageEmailWithException:(NSException*)exception {
//應(yīng)用版本
NSString*version =AppVersion;
if(nil== version) {
version =@"";
}
//設(shè)備版本
NSString*deviceModel = [PublicMethodiphoneType];
//系統(tǒng)版本
NSString*sysVersion = [UIDevicecurrentDevice].systemVersion;
//郵件主題
NSString*subject = [NSStringstringWithFormat:@"[Crash][iOS][%@][%@][%@]", version, sysVersion, deviceModel];
//調(diào)用棧
NSArray*stackSysbolsArray = [[exceptionuserInfo]objectForKey:UncaughtExceptionHandlerAddressesKey];
//崩潰原因
NSString*reason = [exceptionreason];
//崩潰原因
NSString*name = [exceptionname];
//郵件正文
NSString*body = [NSStringstringWithFormat:@"\n----------------------------------------------------\n當(dāng)你看到這個頁面的時候別慌,簡單的描述下剛才的操作,然后郵件我\n\n----------------------------------------------------\n崩潰標(biāo)識:\n\n%@\n----------------------------------------------------\n崩潰原因:\n\n%@\n----------------------------------------------------\n崩潰詳情:\n\n%@\n",
name,
reason,
[stackSysbolsArraycomponentsJoinedByString:@"\n"]];
SKPSMTPMessage*myMessage = [[SKPSMTPMessagealloc]init];
發(fā)件箱
myMessage.fromEmail=@"xxx@163.com";
收件箱
myMessage.toEmail=@"xxxx@qq.com";
抄送
myMessage.bccEmail=@"xxxxx@qq.com";
發(fā)件箱的服務(wù)器主機地址
myMessage.relayHost=@"smtp.163.com";
myMessage.requiresAuth=YES;
if(myMessage.requiresAuth) {
// 發(fā)件箱
myMessage.login=@"xxx@163.com";
// 發(fā)件箱的POT/SMT/..的密碼,必須要去授權(quán)獲取這個密碼,不然用不了郵件發(fā)送功能
myMessage.pass=@"xxxx";
}
myMessage.wantsSecure=YES;//為gmail郵箱設(shè)置smtp.gmail.com
myMessage.subject= subject;
myMessage.delegate=self;
//設(shè)置郵件內(nèi)容
NSDictionary*plainPart = [NSDictionarydictionaryWithObjectsAndKeys:@"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,
body,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
myMessage.parts= [NSArrayarrayWithObjects:plainPart,nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
[myMessagesend];
});
}
- (void)handleException:(NSException*)exception {
[selfvalidateAndSaveCriticalApplicationDataWithException:exception];
NSString*message = [NSStringstringWithFormat:NSLocalizedString(@"如果點擊繼續(xù),程序有可能會出現(xiàn)其他的問題,建議您還是點擊退出按鈕并重新打開\n\n"@"異常原因如下:\n%@\n%@",nil),[exceptionreason],[[exceptionuserInfo]objectForKey:UncaughtExceptionHandlerAddressesKey]];
UIAlertView*alert =[[UIAlertViewalloc]initWithTitle:NSLocalizedString(@"抱歉诫尽,程序出現(xiàn)了異常",nil)
message:message
delegate:self
cancelButtonTitle:NSLocalizedString(@"退出",nil)
otherButtonTitles:NSLocalizedString(@"繼續(xù)",nil),nil];
[alertshow];
CFRunLoopRefrunLoop =CFRunLoopGetCurrent();
CFArrayRefallModes =CFRunLoopCopyAllModes(runLoop);
while(!dismissed) {
for(NSString*modein(__bridgeNSArray*)allModes) {
CFRunLoopRunInMode((__bridgeCFStringRef)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([[exceptionname]isEqual:UncaughtExceptionHandlerSignalExceptionName]) {
kill(getpid(), [[[exceptionuserInfo]objectForKey:UncaughtExceptionHandlerSignalKey]intValue]);
}else{
[exceptionraise];
}
}
- (void)messageSent:(SKPSMTPMessage*)message
{
NSLog(@"恭喜,郵件發(fā)送成功");
}
- (void)messageFailed:(SKPSMTPMessage*)message error:(NSError*)error
{
NSLog(@"不好意思,郵件發(fā)送失敗");
}
@end
voidHandleException(NSException*exception) {
int32_texceptionCount =OSAtomicIncrement32(&UncaughtExceptionCount);
if(exceptionCount >UncaughtExceptionMaximum) {
return;
}
//調(diào)用棧
NSArray*stackSysbolsArray = [exceptioncallStackSymbols];
NSMutableDictionary*userInfo =[NSMutableDictionarydictionaryWithDictionary:[exceptionuserInfo]];
[userInfosetObject:[NSArrayarrayWithArray:stackSysbolsArray]forKey:UncaughtExceptionHandlerAddressesKey];
[[[UncaughtExceptionHandleralloc]init]performSelectorOnMainThread:@selector(handleException:)withObject:
[NSExceptionexceptionWithName:[exceptionname]reason:[exceptionreason]userInfo:userInfo]waitUntilDone:YES];
}
voidSignalHandler(intsignal) {
int32_texceptionCount =OSAtomicIncrement32(&UncaughtExceptionCount);
if(exceptionCount >UncaughtExceptionMaximum) {
return;
}
NSMutableDictionary*userInfo =[NSMutableDictionarydictionaryWithObject:[NSNumbernumberWithInt:signal]forKey:UncaughtExceptionHandlerSignalKey];
NSArray*callStack = [UncaughtExceptionHandlerbacktrace];
[userInfosetObject:callStackforKey:UncaughtExceptionHandlerAddressesKey];
[[[UncaughtExceptionHandleralloc]init]performSelectorOnMainThread:@selector(handleException:)
withObject:[NSExceptionexceptionWithName:UncaughtExceptionHandlerSignalExceptionName
reason:[NSStringstringWithFormat:NSLocalizedString(@"Signal %d was raised.",nil),signal]userInfo:userInfo]
waitUntilDone:YES];
}
voidInstallUncaughtExceptionHandler(void) {
NSSetUncaughtExceptionHandler(&HandleException);
signal(SIGABRT,SignalHandler);
signal(SIGILL,SignalHandler);
signal(SIGSEGV,SignalHandler);
signal(SIGFPE,SignalHandler);
signal(SIGBUS,SignalHandler);
signal(SIGPIPE,SignalHandler);
}
郵件處理
http://blog.csdn.net/u010218226/article/details/53114595
http://blog.csdn.net/aldridge1/article/details/38826035
http://blog.csdn.net/aldridge1/article/details/38826035