iOS中傳送簡訊/發送郵件的實現 韓俊強的部落格
需要引入架構:
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:%@,@13683799303@163.com]]; //cc:抄送對象 subject:主題 body:內容 //NSURL *mailURL2 = [NSURL URLWithString:[NSString stringWithFormat:@mailto:%@?cc = %@&subject = %@&body = %@,@13683799303@163.com,@13683799303@26.com,@郵件,@你好啊!]]; 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:@[@13683799303@163.com,@135895587@qq.com]]; //設定抄送對象, [mailVC setCcRecipients:@[@13683799303@163.com,@135895587@qq.com]]; //設定密送 [mailVC setBccRecipients:@[@13683799303@163.com]]; //設定內容 [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]; }
最終效果:(由於模擬器沒法示範傳送簡訊,所以會出現下面的現象)