##功能html
##分析
非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。(经历过四个技术总监)
以上都有着完整的项目管理系统,包括了任务安排,Bug追踪系统等等,平常工做够用了。当有Bug的时候测试人员须要在手机上手动截图,将图片导入PC后再上传平台上,再由平台发给对应的开发人员。若是有崩溃的Bug还要想着复现,握着数据线插,拿着手机走来走去,是否是很麻烦。因此写了一个小工具,关于信号量的异常捕获,有待往后完善,见笑了。