您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 開發中實現郵件與短信發送的簡單示例

編輯:iOS 相關教學

發送郵件

1. 導入庫檔案:MessageUI.framework。

2. 引入頭檔案。

3. 實作代理 <MFMailComposeViewControllerDelegate> 與 <UINavigationControllerDelegate>。

程式碼示例:

- (void)didClickSendEmailButtonAction {
    if ([MFMailComposeViewController canSendMail] == YES) {
        MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
        // 設置代理(注意:這裡不是 delegate,千萬別忘了)
        mailVC.mailComposeDelegate = self;
        // 收件人
        [mailVC setToRecipients:@[@"someone@gmail.com"]];
        // 抄送
        [mailVC setCcRecipients:@[@"someone@126.com"]];
        // 密送
        [mailVC setBccRecipients:@[@"someone@qq.com"]];
        // 主題
        [mailVC setSubject:@"hello world"];
        [self presentViewController:mailVC animated:YES completion:nil];
        [mailVC setMessageBody:@"郵件內容" isHTML:NO];
    } 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];
}

// 系統發送,模擬器不支持,需用真機測試
- (void)didClickSendSystemEmailButtonAction {
    NSURL *url = [NSURL URLWithString:@"mailto:someone@gmail.com"];
    if ([[UIApplication sharedApplication] canOpenURL:url] == YES) {
        [[UIApplication sharedApplication] openURL:url];
    } else {
        NSLog(@"此設備不支持");
    }
}

發送短信

前面三步的引入設定與郵件發送相同。

// 調用系統 API 發送短信
- (void)didClickSendMessageButtonAction {
    if ([MFMessageComposeViewController canSendText] == YES) {
        MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
        // 設置代理
        messageVC.messageComposeDelegate = self;
        // 發送對象
        messageVC.recipients = @[@"18700000000"];
        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:18600000000"];
    if ([[UIApplication sharedApplication] canOpenURL:url] == YES) {
        [[UIApplication sharedApplication] openURL:url];
    } else {
        NSLog(@"失敗");
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved