功能
- 非Crash Bug 在App內(nèi)可截圖添加描述并發(fā)送
- Crash Bug 在App第二次啟動(dòng)時(shí)提取Crash log添加描述并發(fā)送
分析
非Crash的Bug:字體不對(duì)幽污、顏色不對(duì)、數(shù)據(jù)不對(duì)捅膘、布局不對(duì)煮甥。
Crash Bug:系統(tǒng)Crash、處理signal
場(chǎng)景交互:發(fā)現(xiàn)非Crash Bug時(shí)候搖一搖手機(jī)萎津,彈出郵件精绎,圖片帶入郵件挺份,點(diǎn)擊發(fā)送即可。有Crash Bug的時(shí)候第二次啟動(dòng)App欲间,彈出郵件楚里,Crash log帶入郵件,點(diǎn)擊發(fā)送即可猎贴。
需要用到NSSetUncaughtExceptionHandler班缎,MFMailComposeViewController,沙盒嘱能,NSFileManager吝梅。
實(shí)現(xiàn)
截圖的功能虱疏,考慮到并不是所有的頁(yè)面都需要使用所以寫(xiě)在了分類里惹骂。需要用的時(shí)候直接引入頭文件即可。
//這三個(gè)方法分別在搖一搖的時(shí)候回調(diào)用做瞪,開(kāi)始对粪,需要,結(jié)束装蓬。他們的父類是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];
}
發(fā)送郵件的功能,也寫(xiě)在了分類里面牍帚,需要用的時(shí)候引入即可儡遮。
@interface UIViewController (send)<MFMailComposeViewControllerDelegate>
//發(fā)送郵件的方法,傳入標(biāo)題暗赶,描述信息鄙币,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:@"不能調(diào)用郵箱" andDesc:@"請(qǐng)嘗試下載App原生郵箱,并配置"];
}
}
//MFMailComposeViewControllerDelegate的代理方法蹂随,可以在這個(gè)方法里面寫(xiě)一些回調(diào)
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result) {
case MFMailComposeResultSent:
[self alertView:@"發(fā)送成功" 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:@"取消發(fā)送" andDesc:nil];
break;
default:
[self alertView:@"為什么不發(fā)送" andDesc:nil];
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
異常捕獲
這兩個(gè)為函數(shù)方法十嘿,導(dǎo)入類名,直接可調(diào)用不用初始化
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時(shí)間:%@\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
檢測(cè)Crash log 功能在App打開(kāi)的第一個(gè)頁(yè)面去調(diào)用就好
-(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];
}
以上代碼均為局部代碼岳锁,具體代碼請(qǐng)移步github
為什么要寫(xiě)
此處廢話绩衷,可忽略
最近找工作面技術(shù)的時(shí)候經(jīng)常會(huì)聊到App中bug的處理,我前公司W(wǎng)eb端業(yè)務(wù)繁重領(lǐng)導(dǎo)并不太關(guān)心App激率,只有一個(gè)全局的異常捕獲還是我軟磨硬泡加進(jìn)去的咳燕。我只有實(shí)話實(shí)說(shuō),告訴他我們的Bug統(tǒng)計(jì)平臺(tái)乒躺,又胡謅一些個(gè)人意見(jiàn)可加入第三方Bug管理工具(OneAPM招盲,Bugly)。這并不是滿意的答案聪蘸,他們的Bug是自己寫(xiě)的工具宪肖,所以我只能繼續(xù)求職中表制。
我們前公司的Bug管理用過(guò)禪道、OSChina控乾、Jira么介。(經(jīng)歷過(guò)四個(gè)技術(shù)總監(jiān))
- 禪道
我們之前的測(cè)試?yán)洗笥霉痉?wù)器搭建的,中規(guī)中矩蜕衡,我覺(jué)得很好用啊壤短,配合著jekins還能去看后臺(tái)的日志。 - OSChina
第三個(gè)技術(shù)總監(jiān)用的慨仿,省事久脯,拿過(guò)來(lái)直接用就好,不用搭建服務(wù)器什么的镰吆。 - jira
第四任技術(shù)總監(jiān)搭建的帘撰,功能最為強(qiáng)大,正版的jira很貴万皿,我們用的破解版摧找。
以上都有著完整的項(xiàng)目管理系統(tǒng),包括了任務(wù)安排牢硅,Bug追蹤系統(tǒng)等等蹬耘,日常工作夠用了。當(dāng)有Bug的時(shí)候測(cè)試人員需要在手機(jī)上手動(dòng)截圖减余,將圖片導(dǎo)入PC后再上傳平臺(tái)上综苔,再由平臺(tái)發(fā)給對(duì)應(yīng)的開(kāi)發(fā)人員。如果有崩潰的Bug還要想著復(fù)現(xiàn)位岔,握著數(shù)據(jù)線插如筛,拿著手機(jī)走來(lái)走去,是不是很麻煩赃承。所以寫(xiě)了一個(gè)小工具妙黍,關(guān)于信號(hào)量的異常捕獲,有待日后完善瞧剖,見(jiàn)笑了拭嫁。