有一段时间没有更新博客了,今天更新一篇关于最近工作中用到的一个功能,先简单描述一下:我们知道,测试人员在测试客户端产品时,当出现问题或者BUG的时候,都得先对页面截图,然后从相册中选择截图,加上一段描述放到TD库或者直接通过邮件发给开发人员,以方便开发人员进行修改,过程繁琐。通过下面功能的介绍,您可以很方便的让测试人员遇到BUG的时候,摇一摇设备,然后自动截图并保存到应用的沙盒之中。截图保存到沙盒之后,您可以自定义一个问题反馈的页面,对问题进行描述(描述可使用第三方平台,如讯飞语音,实现语音录入的效果)之后,将描述信息和页面截图一起发送给开发人员(可以是邮件,也可以是直接发送到后台)。
下面来看看功能是怎么实现的。
首先,定义了一个category:ShakeAndCutter
UIViewController+ShakeAndCutter.h文件源码:
#import <UIKit/UIKit.h>
@interface UIViewController (ShakeAndCutter)
@end
UIViewController+ShakeAndCutter.m文件源码:
1#import"UIViewController+ShakeAndCutter.h"2#import<QuartzCore/QuartzCore.h>3#import"TestFeedbackViewController.h"45@implementationUIViewController (ShakeAndCutter)67-(BOOL)canBecomeFirstResponder8{9returnYES;10}1112#PRagmamark - 摇一摇动作处理1314- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event15{16NSLog(@"began");17}1819- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event20{21NSLog(@"cancel");22}2324- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event25{26NSLog(@"end");27[self cutterViewToDocument];2829//这里是自定义的问题反馈页面,也可以直接跳转到系统邮件发送的页面30TestFeedbackViewController *testFeedbackViewController = [[[TestFeedbackViewController alloc] initWithNibName:@"TestFeedbackViewController"bundle:nil] autorelease];31[self.navigationController pushViewController:testFeedbackViewController animated:YES];32}3334#pragmamark - 截屏处理3536- (void)cutterViewToDocument37{38UIWindow *screenWindow =[[UIapplicationsharedApplication] keyWindow];3940UIGraphicsBeginImageContext(screenWindow.frame.size);41[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];42UIImage *screenShot =UIGraphicsGetImageFromCurrentImageContext();43UIGraphicsEndImageContext();4445NSData *screenShotPNG =UIImagePNGRepresentation(screenShot);46NSError *error =nil;47[screenShotPNG writeToFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"error.png"] options:NSAtomicWrite error:&error];48}4950@end
至此,摇一摇和截图功能就已经完成了,那么在应用中怎么使用呢?
对于IOS6.0以上的设备,只需要在项目的pch文件中引进头文件#import "UIViewController+ShakeAndCutter.h"即可(我在IOS6.0.1、IOS6.1.5、IOS7.0.6设备上已经测试过),这样测试工作完成之后,您只需要将这行代码注释或者删除就可以了;对于IOS6.0以下的设备,在需要实现摇一摇效果的页面,有可能要加入如下代码(手头没有IOS6.0以下的设备,如果大家有的话,帮忙验证一下,非常感谢):1.viewDidLoad方法中,添加两行代码:
1[[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];2[self becomeFirstResponder];
2.viewDidAppear方法中,添加一行代码:
1[self becomeFirstResponder];
3.viewDidDisappear方法中,添加一行代码:
1[self resignFirstResponder];
当然,以上的功能,您也可以实现其他的效果:比如说用户问题的反馈等等,具体可以根据应用的需求而定。
这里,再附上发送到邮件功能的示例代码:
MailViewController.h文件源码:
1#import<UIKit/UIKit.h>2#import<MessageUI/MessageUI.h>3#import<MessageUI/MFMailComposeViewController.h>45@interfaceMailViewController : UIViewController<MFMailComposeViewControllerDelegate,6MFMessageComposeViewControllerDelegate>7{8UIButton *shareToMailButton;9}1011@end
MailViewController.m文件源码:
1#import"MailViewController.h"2#import"Constants.h"34@interfaceMailViewController ()56@end78@implementationMailViewController910- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil11{12self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];13if(self) {14//Custom initialization15}16returnself;17}1819- (void)viewDidLoad20{21[super viewDidLoad];22//Do any additional setup after loading the view.2324self.title =@"Bug反馈";2526shareToMailButton = [self buttonWithFrame:CGRectMake(20,100,280,40) action:@selector(btnClicked:) withTag:1000];27[shareToMailButton setTitle:@"Bug反馈"forState:UIControlStateNormal];28}2930- (void)didReceiveMemoryWarning31{32[super didReceiveMemoryWarning];33//Dispose of any resources that can be recreated.34}3536#pragmamark - 自定义方法3738- (void)btnClicked:(id)sender39{40UIButton *btnSender = (UIButton *)sender;4142switch(btnSender.tag)43{44case1000://分享到邮箱45{46[self showMailPicker];47
break;48}49default:50
break;51}52}5354/*******************************************************************************55* 方法名称:buttonWithFrame:action:56* 功能描述:初始化页面按钮,并添加到页面视图57* 输入参数:58* 输出参数:59******************************************************************************/60- (UIButton *)buttonWithFrame:(CGRect)frame action:(SEL)action withTag:(int)tag61{62UIImage *buttonBackgroundImage = [[UIImage imageNamed:@"button_background.png"] stretchableImageWithLeftCapWidth:5topCapHeight:5];63UIImage *disabledButtonBackgroundImage = [[UIImage imageNamed:@"button_background_disabled.png"] stretchableImageWithLeftCapWidth:5topCapHeight:5];6465UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];66button.frame =frame;67[button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal];68[button setBackgroundImage:disabledButtonBackgroundImage forState:UIControlStateDisabled];69[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];70[button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];71[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];72button.tag =tag;73[self.view addSubview:button];7475returnbutton;76}7778- (void)showMailPicker79{80Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));8182if(mailClass !=nil)83{84if([mailClass canSendMail])85{86[self displayMailComposerSheet];87}88else89{90UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持邮件功能"delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];91[alert show];92[alert release];93}94}95else96{9798}99100}101102- (void)displayMailComposerSheet103{104MFMailComposeViewController *picker =[[MFMailComposeViewController alloc] init];105106//设置picker的委托方法,完成之后会自动调用成功或失败的方法107picker.mailComposeDelegate =self;108//添加主题109[picker setSubject:@"Bug反馈"];110//添加收件人111NSArray *toRecipients = [NSArray arrayWithObject:@"279352257@QQ.com"];112//说明:也可以添加多个收件人,代码如下所示:113//NSArray *toRecipients = [NSArray arrayWithObjects:@"one@qq.com",@"two@qq.com",nil];114//添加抄送115//NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@qq.com",@"third@qq.com", nil];116//添加密送117//NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@qq.com"];118119[picker setToRecipients:toRecipients];120//[picker setCcRecipients:ccRecipients];121//[picker setBccRecipients:bccRecipients];122//发送图片附件(其他格式的附件,可以都先转化称NSData类型,然后设置相应的mimeType即可,如txt类型为@"text/txt",doc类型为@"text/doc",pdf类型为@"file/pdf"等等)123NSData *myData = [NSData dataWithContentsOfFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"error.png"]];124[picker addAttachmentData:myData mimeType:@"image/jpeg"fileName:@"error.png"];125NSString *emailBody = [NSString stringWithFormat:@"<p>文字没有全部显示</p>"];126127//直接在HTML代码中写入图片的地址128//NSString *emailBody = [NSString stringWithFormat:@"<img src='http://p2.so.qhimg.com/t0130e3288d86929b97.jpg' /><p>我分享了图片</p>"];129130[picker setMessageBody:emailBody isHTML:YES];131[self presentModalViewController:picker animated:YES];132[picker release];133}134135136- (void)mailComposeController:(MFMailComposeViewController*)controller137didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error138{139switch(result)140{141caseMFMailComposeResultCancelled:142NSLog(@"Result: Mail sending canceled");//邮件发送取消143
break;144caseMFMailComposeResultSaved:145NSLog(@"Result: Mail saved");//邮件保存成功146
break;147caseMFMailComposeResultSent:148NSLog(@"Result: Mail sent");//邮件发送成功149
break;150caseMFMailComposeResultFailed:151NSLog(@"Result: Mail sending failed");//邮件发送失败152
break;153default:154NSLog(@"Result: Mail not sent");155
break;156}157[self dismissModalViewControllerAnimated:YES];158}159160- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result161{162NSLog(@"messageComposeViewController");163}164165@end