1.首先添加 MessageUI.framework 框架
2. 引入框架
在类的头部
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
3. 实现接口
<MFMailComposeViewControllerDelegate>
- (void)handleDoubleTap:(UIGestureRecognizer *)doubleTap{
model.isShowUDID = !model.isShowUDID;
if (doubleTap.state == UIGestureRecognizerStateEnded) {
// 展示设备udid
if (model.isShowUDID) {
if (!label) {
label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame) - 80, CGRectGetWidth(self.view.frame), 80)];
label.numberOfLines = 0;
label.font = [UIFont systemFontOfSize:15.f];
label.textAlignment = NSTextAlignmentCenter;
label.text = [ManagerCoca udid];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
[self.view addSubview:label];
}
label.hidden = NO;
Class mailClass = NSClassFromString(@"MFMailComposeViewController");
if (mailClass!=nil) {
if ([mailClass canSendMail]) {
[self displayComposerSheet:[ManagerCoca udid]];
}
}else{
[self launchMailAppOnDevice:[ManagerCoca udid]];
}
}else{
label.hidden = YES;
}
}
}
- (void)launchMailAppOnDevice:(NSString *)udid{
NSString *recipients = @"example@qq.com&subject=!设备唯一识别号";
//@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";
NSString *emailBody = [NSString
stringWithFormat:@"&body=!Version: %@\nUniversallyUniqueIdentifier: %@\n\n",
[[UIDevice currentDevice] systemVersion],udid];
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, emailBody];
email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
-(void)displayComposerSheet:(NSString *)udid
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];/*MFMailComposeViewController邮件发送选择器*/
picker.mailComposeDelegate = self;
[picker setSubject:@"设备唯一识别号"];/*emailpicker标题主题行*/
NSArray *toRecipients = [NSArray arrayWithObject:@"example@qq.com"];
[picker setToRecipients:toRecipients];
NSString *emailBody = [NSString
stringWithFormat:@"Version: %@\nUniversallyUniqueIdentifier: %@\n\n",
[[UIDevice currentDevice] systemVersion],udid];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
- (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
message:msg
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSString *msg;
switch (result)
{
case MFMailComposeResultCancelled:
msg = @"邮件发送取消";
break;
case MFMailComposeResultSaved:
msg = @"邮件保存成功";
[self alertWithTitle:nil msg:msg];
break;
case MFMailComposeResultSent:
msg = @"邮件发送成功";
[self alertWithTitle:nil msg:msg];
break;
case MFMailComposeResultFailed:
msg = @"邮件发送失败";
[self alertWithTitle:nil msg:msg];
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}