iOS開發中實現郵件和簡訊發送的簡單樣本_IOS

來源:互聯網
上載者:User

發送郵件
1.匯入庫檔案:MessageUI.framework
2.引入標頭檔
3.實現代理<MFMailComposeViewControllerDelegate> 和 <UINavigationControllerDelegate>
程式碼範例:

複製代碼 代碼如下:

- (void)didClickSendEmailButtonAction{ 
 
    if ([MFMailComposeViewController canSendMail] == YES) { 
         
        MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init]; 
        //  設定代理(與以往代理不同,不是"delegate",千萬不能忘記呀,代理有3步) 
        mailVC.mailComposeDelegate = self; 
        //  收件者 
        NSArray *sendToPerson = @[@"humingtao2014@gmail.com"]; 
        [mailVC setToRecipients:sendToPerson]; 
        //  抄送 
        NSArray *copyToPerson = @[@"humingtao2013@126.com"]; 
        [mailVC setCcRecipients:copyToPerson]; 
        //  密送 
        NSArray *secretToPerson = @[@"563821250@qq.com"]; 
        [mailVC setBccRecipients:secretToPerson]; 
        //  主題  
        [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:@"humingtao2014@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]; 
        //  設定代理<MFMessageComposeViewControllerDelegate> 
        messageVC.messageComposeDelegate = self; 
        //  發送To Who 
        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(@"失敗"); 
    } 
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.