//該方法在不退出應用程序的前提下調用短信和郵箱,以下內容請在真機測試
//導入框架MessageUI.framework
#import "ViewController.h"
//首先導入頭文件
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MFMessageComposeViewController.h>
//代理
@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];
}