本身動手寫一個Bug管理工具

timg.jpeg
timg.jpeg

##功能html

  • 非Crash Bug 在App內可截圖添加描述併發送
  • Crash Bug 在App第二次啓動時提取Crash log添加描述併發送

##分析
非Crash的Bug:字體不對、顏色不對、數據不對、佈局不對。
Crash Bug:系統Crash、處理signalgit

場景交互:發現非Crash Bug時候搖一搖手機,彈出郵件,圖片帶入郵件,點擊發送便可。有Crash Bug的時候第二次啓動App,彈出郵件,Crash log帶入郵件,點擊發送便可。github

須要用到NSSetUncaughtExceptionHandler,MFMailComposeViewController,沙盒,NSFileManager。bash

##實現
截圖的功能,考慮到並非全部的頁面都須要使用因此寫在了分類裏。須要用的時候直接引入頭文件便可。服務器

//這三個方法分別在搖一搖的時候回調用,開始,須要,結束。他們的父類是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];
}複製代碼

異常捕獲dom

這兩個爲函數方法,導入類名,直接可調用不用初始化
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還要想着復現,握着數據線插,拿着手機走來走去,是否是很麻煩。因此寫了一個小工具,關於信號量的異常捕獲,有待往後完善,見笑了。

相關文章
相關標籤/搜索