您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發實作:快速實現郵件與短信發送功能

編輯:iOS 開發綜合

一、發送郵件

步驟很簡單:先匯入 MessageUI.framework 庫文件,引入對應頭文件,再讓控制器實作 <MFMailComposeViewControllerDelegate> 與 <UINavigationControllerDelegate> 兩個代理即可。

需要特別留意的是,郵件編輯器的代理並非命名為 delegate,而是 mailComposeDelegate,設定時千萬別寫錯。以下為完整示範:

- (void)didClickSendEmailButtonAction {
    if ([MFMailComposeViewController canSendMail] == YES) {
        MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
        mailVC.mailComposeDelegate = self; // 注意:不是 delegate
        [mailVC setToRecipients:@[@"humingtao2014@gmail.com"]];   // 收件人
        [mailVC setCcRecipients:@[@"humingtao2013@126.com"]];     // 副本
        [mailVC setBccRecipients:@[@"563821250@qq.com"]];         // 密件副本
        [mailVC setSubject:@"hello world"];                       // 主題
        [mailVC setMessageBody:@"魑魅魍魉,哈哈呵呵嘿嘿霍霍" isHTML:NO];
        [self presentViewController:mailVC animated:YES completion:nil];
    } else {
        NSLog(@"此裝置不支援郵件發送");
    }
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
        didFinishWithResult:(MFMailComposeResult)result
                      error:(NSError *)error {
    switch (result) {
        case MFMailComposeResultCancelled: NSLog(@"取消發送"); break;
        case MFMailComposeResultFailed:    NSLog(@"發送失敗"); break;
        case MFMailComposeResultSaved:     NSLog(@"儲存草稿"); break;
        case MFMailComposeResultSent:      NSLog(@"發送成功"); break;
        default: break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

若想直接呼叫系統郵件應用,可用 openURL: 打開 mailto 連結;但此方法在模擬器上並不支援,需以真機測試。

二、發送短信

前三步的引入與配置方式和郵件完全相同。核心在於使用 MFMessageComposeViewController,並實作 <MFMessageComposeViewControllerDelegate> 代理:

- (void)didClickSendMessageButtonAction {
    if ([MFMessageComposeViewController canSendText] == YES) {
        MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
        messageVC.messageComposeDelegate = self;
        messageVC.recipients = @[@"18757289870"];  // 收件對象
        messageVC.body = @"hello world";           // 內容
        [self presentViewController:messageVC animated:YES completion:nil];
    } else {
        NSLog(@"此裝置不支援");
    }
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
                 didFinishWithResult:(MessageComposeResult)result {
    switch (result) {
        case MessageComposeResultCancelled: NSLog(@"取消發送"); break;
        case MessageComposeResultFailed:    NSLog(@"發送失敗"); break;
        case MessageComposeResultSent:      NSLog(@"發送成功"); break;
        default: break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 呼叫系統應用發送短信
- (void)didClickSendMessage2ButtonAction {
    NSURL *url = [NSURL URLWithString:@"sms:18656348970"];
    if ([[UIApplication sharedApplication] canOpenURL:url] == YES) {
        [[UIApplication sharedApplication] openURL:url];
    } else {
        NSLog(@"失敗");
    }
}

無論是郵件還是短信,都建議先透過 canSendMail 或 canSendText 判斷裝置是否支援,再決定後續流程,這樣能有效避免在缺乏相應能力的裝置上出現異常。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved