MessageUI.framework
布局如下:
短信和郵件:
#import ViewController.h #import@interface ViewController () //遵循協議 @end @implementation ViewController
//短信功能 - (IBAction)messageButtonAction:(UIButton *)sender { #pragma mark 程序外發送短信 /* //定義打開短信的url, 關鍵字: sms: NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@sms://%@,@10086]]; //判斷程序是否可以調用打開短信功能 if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url]; }else{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@提示 message:@您的設備不支持此短信功能 delegate:self cancelButtonTitle:@確定 otherButtonTitles:nil, nil]; [alert show]; } */ /* 用openURL來打開程序中的短信功能, 需要用到關鍵字: sms:, 後面加上要發送的電話就可以了; 缺點:1.這個方法會跳出我們正在運行的程序,打開系統的短信界面, 但當用戶關閉短信後, 無法回到程序. 2.這個方法我們只能定義要發送的手機號, 無法編輯發送的短信內容; */ }
#pragma mark 程序內發送短信
/*
為了彌補上述的兩個方法的不足,需要另一種使用短信功能的方法:程序內使用短信功能.
*/
//1.添加短信所需要的框架: MessageUI.framework
//2.引入頭文件,實現如下代碼
//3.判斷是否可以發短信
- (IBAction)messageButtonAction:(UIButton *)sender { #pragma mark 程序外發送短信 BOOL canSendMessage = [MFMessageComposeViewController canSendText]; if (canSendMessage) { //創建短信視圖控制器 MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc]init]; //設置代理 messageVC.messageComposeDelegate = self; //設置短信內容 messageVC.body = @來一條信息; //設置電話, 是一個數組, 可以設置多個電話, 實現群發功能 messageVC.recipients = @[@10086,@10010]; //打開短信功能, 通過這個方法會在程序內打開一個短信界面; [self presentViewController:messageVC animated:YES completion:nil]; }else{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@提示 message:@您的設備不支持此短信功能 delegate:self cancelButtonTitle:@確定 otherButtonTitles:nil, nil]; [alert show]; } }
信息的代理方法:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ //MessageComposeResult 的枚舉值: // MessageComposeResultCancelled, //取消發送短信功能 // MessageComposeResultSent, //發送短信 // MessageComposeResultFailed //發送失敗 if (result == MessageComposeResultCancelled || result == MessageComposeResultSent) { [self dismissViewControllerAnimated:YES completion:nil]; } }
郵件功能:
//郵件功能 - (IBAction)mailButtonAction:(UIButton *)sender { #pragma mark 程序外發送郵件 /* //打開系統郵件頁面, mailto: NSURL *mailURL = [NSURL URLWithString:[NSString stringWithFormat:@mailto:%@,@[email protected]]]; //cc:抄送對象 subject:主題 body:內容 //NSURL *mailURL2 = [NSURL URLWithString:[NSString stringWithFormat:@mailto:%@?cc = %@&subject = %@&body = %@,@[email protected],@[email protected],@郵件,@你好啊!]]; if ([[UIApplication sharedApplication] canOpenURL:mailURL]) { [[UIApplication sharedApplication] openURL:mailURL]; }else{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@提示 message:@您的設備不支持郵件功能 delegate:self cancelButtonTitle:@確定 otherButtonTitles:nil, nil]; [alert show]; } */ /* 此方法來發送郵件同上述短信一樣,也會跳出程序,調用系統的郵件界面; */ #pragma mark 程序內發送郵件 //判斷是否可以發送郵件 BOOL canSendMail = [MFMailComposeViewController canSendMail]; if (canSendMail) { //創建郵件視圖控制器 MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc]init]; //設置接收郵件人, 數組,可以實現群發 [mailVC setToRecipients:@[@[email protected],@[email protected]]]; //設置抄送對象, [mailVC setCcRecipients:@[@[email protected],@[email protected]]]; //設置密送 [mailVC setBccRecipients:@[@[email protected]]]; //設置內容 [mailVC setMessageBody:@很高興認識你 isHTML:NO]; //設置代理 mailVC.mailComposeDelegate = self; //打開郵件功能 [self presentViewController:mailVC animated:YES completion:nil]; }else{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@提示 message:@您的設備不支持郵件功能 delegate:self cancelButtonTitle:@確定 otherButtonTitles:nil, nil]; [alert show]; } }
郵件代理的方法:
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ // MFMailComposeResultCancelled, 取消發送 // MFMailComposeResultSaved, 保存 // MFMailComposeResultSent, 發送 // MFMailComposeResultFailed 發送失敗 switch (result) { case MFMailComposeResultCancelled: NSLog(@取消發送); break; case MFMailComposeResultSaved: NSLog(@保存); break; case MFMailComposeResultSent: NSLog(@發送成功); break; case MFMailComposeResultFailed: NSLog(@失敗); break; default: break; } [self dismissViewControllerAnimated:YES completion:nil]; }