//该方法在不退出应用程序的前提下调用短信和邮箱,以下内容请在真机测试
//导入框架MessageUI.framework
#import "ViewController.h"
//首先导入头文件
#import
#import
//代理
@interface ViewController ()<MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
@end
@implementation ViewController
//邮件按钮方法实现
- (void)mail:(id)sender {
//判断设备是否支持应用内发送邮件功能
if ([MFMailComposeViewController canSendMail]) {
//在应用内发送邮件
//创建邮件controller
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
//设置邮件代理
mailPicker.mailComposeDelegate = self;
//邮件主题
[mailPicker setSubject:@"Send WebView ScreenShot"];
//设置发送给谁,参数是NSarray,设置发送给两个邮箱
[mailPicker setToRecipients:[NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]];
//可以添加抄送
[mailPicker setCcRecipients:[NSArray arrayWithObject:@"[email protected]"]];
//可以添加暗抄送
[mailPicker setBccRecipients:[NSArray arrayWithObject:@"[email protected]"]];
//邮件正文
[mailPicker setMessageBody:@"WebShotScreen n in Attachment!" isHTML:NO];
//发送图片附件
//第一个图片名字是本地要选择发送的图片的名字, 第二个图片的名字是邮件里发送时显示的图片名字
NSString *pathImage = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"jpg"];
NSData *dataImage = [NSData dataWithContentsOfFile:pathImage];
[mailPicker addAttachmentData:dataImage mimeType:@"image/jpg" fileName:@"1.jpg"];
//发送txt文本附件
NSString *pathText = [[NSBundle mainBundle] pathForResource:@"tv" ofType:@"txt"];
NSData *dataText = [NSData dataWithContentsOfFile:pathText];
[mailPicker addAttachmentData:dataText mimeType:@"text/txt" fileName:@"aa.txt"];
//发送doc文本附件
NSString *pathDoc = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"doc"];
NSData *dataDoc = [NSData dataWithContentsOfFile:pathDoc];
[mailPicker addAttachmentData:dataDoc mimeType:@"text/doc" fileName:@"MyText.doc"];
//发送pdf文档附件
NSString *pathPdf = [[NSBundle mainBundle] pathForResource:@"CodeSigningGuide"ofType:@"pdf"];
NSData *dataPdf = [NSData dataWithContentsOfFile:pathPdf];
[mailPicker addAttachmentData:dataPdf mimeType:@"file/pdf"fileName:@"rainy.pdf"];
//把当前controller变为邮件controller
[self presentModalViewController:mailPicker animated:YES];
}else{
//如果该设备不支持在不退出程序的前提下调用邮件,则会推出应用程序并调用系统邮件,mailto://为固定写法后面加邮箱地址
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://[email protected]"]];
}
}
//实现 MFMailComposeViewControllerDelegate
//发送结果
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
NSString *mes = nil;
switch (result)
{
case MFMailComposeResultCancelled:
mes = @"取消编辑邮件";
break;
case MFMailComposeResultSaved:
mes = @"成功保存邮件";
break;
case MFMailComposeResultSent:
mes = @"点击发送,将邮件放到队列中,还没发送";
break;
case MFMailComposeResultFailed:
mes = @"试图保存或者发送邮件失败";
break;
default:
break;
}
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提醒" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
[alter show];
[self dismissModalViewControllerAnimated:YES];
}
//短信按钮方法实现
- (IBAction)message:(id)sender {
//判断设备是否支持应用内发送短信功能
if ([MFMessageComposeViewController canSendText]) {
//在应用内发送短信
{
//初始化
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
//代理
picker.messageComposeDelegate = self;
picker.navigationBar.tintColor = [UIColor blackColor];
//短信内容
picker.body = @"1111111111111111";
//设置发送给谁
picker.recipients = [NSArray arrayWithObject:@"13300000000"];
//推到发送试图控制器
[self presentModalViewController:picker animated:YES];
}
}
else {
//如果该设备不支持在不退出程序的前提下调用短信,则会推出应用程序并调用系统短信,mailto://为固定写法后面加手机号码
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];
}
}
//实现 MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
NSString *mes = nil;
switch (result) {
case MessageComposeResultCancelled:
mes = @"取消编辑短信";
break;
case MessageComposeResultSent:
mes = @"点击发送,将短信放到队列中,还没发送";
break;
case MessageComposeResultFailed:
mes = @"发送短信失败";
break;
default:
break;
}
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提醒" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok", nil];
[alter show];
[self dismissModalViewControllerAnimated:YES];
}