timg.jpeg
功能
- 非Crash Bug 在App內可截圖添加描述并發送
- Crash Bug 在App第二次啟動時提取Crash log添加描述并發送
分析
非Crash的Bug:字體不對、顏色不對、數據不對、布局不對。
Crash Bug:系統Crash、處理signal
場景交互:發現非Crash Bug時候搖一搖手機,彈出郵件,圖片帶入郵件,點擊發送即可。有Crash Bug的時候第二次啟動App,彈出郵件,Crash log帶入郵件,點擊發送即可。
需要用到NSSetUncaughtExceptionHandler,MFMailComposeViewController,沙盒,NSFileManager。
實現
截圖的功能,考慮到并不是所有的頁面都需要使用所以寫在了分類里。需要用的時候直接引入頭文件即可。
//這三個方法分別在搖一搖的時候回調用,開始,需要,結束。他們的父類是UIResponsder在UIKit中。
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
[self screenShot];
}
-(void)screenShot
{
UIWindow *screen = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContext(screen.frame.size);
[screen.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsGetCurrentContext();
NSData *screenData = UIImagePNGRepresentation(image);
[screenData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"] atomically:YES];
}
發送郵件的功能,也寫在了分類里面,需要用的時候引入即可。
@interface UIViewController (send)<MFMailComposeViewControllerDelegate>
//發送郵件的方法,傳入標題,描述信息,data, 接收人
-(void)sendMail:(MFMailComposeViewController*)mf andSubject:(NSString*)subject andMessageBody:(NSString*)message andData:(NSData*)data andRecipients:(NSArray*)recipients
{
if([MFMailComposeViewController canSendMail]){
mf.mailComposeDelegate = self;
[mf setSubject:subject];
[mf setToRecipients:recipients];
[mf addAttachmentData:data mimeType:@"image/jpeg" fileName:@"error"];
[mf setMessageBody:message isHTML:YES];
[self presentViewController:mf animated:YES completion:nil];
}else{
[self alertView:@"不能調用郵箱" andDesc:@"請嘗試下載App原生郵箱,并配置"];
}
}
//MFMailComposeViewControllerDelegate的代理方法,可以在這個方法里面寫一些回調
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result) {
case MFMailComposeResultSent:
[self alertView:@"發送成功" andDesc:nil];
self.success();
break;
case MFMailComposeResultSaved:
[self alertView:@"保存成功" andDesc:nil];
break;
case MFMailComposeResultFailed:
self.faild();
[self alertView:error.domain andDesc:[NSString stringWithFormat:@"%@",error.userInfo]];
break;
case MFMailComposeResultCancelled:
[self alertView:@"取消發送" andDesc:nil];
break;
default:
[self alertView:@"為什么不發送" andDesc:nil];
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
異常捕獲
這兩個為函數方法,導入類名,直接可調用不用初始化
void CrashExceptionHandler(void)
{
NSSetUncaughtExceptionHandler(&ExceptionLog);
}
void ExceptionLog(NSException *exception)
{
NSDate *date_current = [NSDate date];
NSDictionary *dictInfo = [[NSBundle mainBundle]infoDictionary];
NSString *name_App = [dictInfo objectForKey:@"CFBundleDisplayName"];
NSString *verson_App = [dictInfo objectForKey:@"CFBundleShortVersionString"];
NSString *build_App = [dictInfo objectForKey:@"CFBundleVersion"];
NSArray *ecp = exception.callStackSymbols;
NSString *reason = [exception reason];
NSString *name = [exception name];
NSString *exceptionInfo = [NSString stringWithFormat:
@"\n\n ******************************異常日志****************************** \n時間:%@\nApp名稱:%@\nApp版本:%@\nBuild版本:%@\n異常名稱:%@\n異常原因:%@\n堆棧信息:%@",date_current,name_App,verson_App,build_App,name,reason,ecp];
[CrashHandler saveLog:exceptionInfo andDate:date_current];
#ifdef DEBUG
NSLog(@"%@",exceptionInfo);
#else
#endif
}
@implementation CrashHandler
+(void)saveLog:(NSString *)crashLog andDate:(NSDate *)date
{
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingString:@"/Crash"];
if(![[NSFileManager defaultManager]fileExistsAtPath:path])
{
[[NSFileManager defaultManager]createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *logPath = [path stringByAppendingFormat:@"/%@.log",date];
[crashLog writeToFile:logPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
@end
檢測Crash log 功能在App打開的第一個頁面去調用就好
-(void)crashLog
{
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingString:@"/Crash"];
NSFileManager *mf = [NSFileManager defaultManager];
if(![mf fileExistsAtPath:path])
{
return;
}
NSArray *array = [mf contentsOfDirectoryAtPath:path error:nil];
}
以上代碼均為局部代碼,具體代碼請移步github
為什么要寫
此處廢話,可忽略
最近找工作面技術的時候經常會聊到App中bug的處理,我前公司Web端業務繁重領導并不太關心App,只有一個全局的異常捕獲還是我軟磨硬泡加進去的。我只有實話實說,告訴他我們的Bug統計平臺,又胡謅一些個人意見可加入第三方Bug管理工具(OneAPM,Bugly)。這并不是滿意的答案,他們的Bug是自己寫的工具,所以我只能繼續求職中。
我們前公司的Bug管理用過禪道、OSChina、Jira。(經歷過四個技術總監)
- 禪道
我們之前的測試老大用公司服務器搭建的,中規中矩,我覺得很好用啊,配合著jekins還能去看后臺的日志。 - OSChina
第三個技術總監用的,省事,拿過來直接用就好,不用搭建服務器什么的。 - jira
第四任技術總監搭建的,功能最為強大,正版的jira很貴,我們用的破解版。
以上都有著完整的項目管理系統,包括了任務安排,Bug追蹤系統等等,日常工作夠用了。當有Bug的時候測試人員需要在手機上手動截圖,將圖片導入PC后再上傳平臺上,再由平臺發給對應的開發人員。如果有崩潰的Bug還要想著復現,握著數據線插,拿著手機走來走去,是不是很麻煩。所以寫了一個小工具,關于信號量的異常捕獲,有待日后完善,見笑了。