你好,歡迎來到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(@"失敗"); 
    } 
 

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