你好,歡迎來到IOS教程網

 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",萬萬不克不及忘卻呀,署理有3步) 
        mailVC.mailComposeDelegate = self; 
        //  收件人 
        NSArray *sendToPerson = @[@"[email protected]"]; 
        [mailVC setToRecipients:sendToPerson]; 
        //  抄送 
        NSArray *copyToPerson = @[@"[email protected]"]; 
        [mailVC setCcRecipients:copyToPerson]; 
        //  密送 
        NSArray *secretToPerson = @[@"[email protected]"]; 
        [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:@"[email protected]"]; 
    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(@"掉敗"); 
    } 
 

【iOS開辟中完成郵件和短信發送的簡略示例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

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