1. 程式人生 > >IOS自帶Email的兩種方法

IOS自帶Email的兩種方法

IOS系統框架提供的兩種傳送Email的方法:openURL 和 MFMailComposeViewController。藉助這兩個方法,我們可以輕鬆的在應用里加入如使用者反饋這類需要傳送郵件的功能。

1.openURL

使用openURL呼叫系統郵箱客戶端是我們在IOS3.0以下實現發郵件功能的主要手段。我們可以通過設定url裡的相關引數來指定郵件的內容,不過其缺點很明顯,這樣的過程會導致程式暫時退出。下面是使用openURL來發郵件的一個小例子:
  1. #pragma mark - 使用系統郵件客戶端傳送郵件 
  2. -(void)launchMailApp   
  3. {     
  4.     NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];   
  5. //新增收件人 
  6.     NSArray *toRecipients = [NSArray arrayWithObject: @
    "[email protected]"];   
  7.     [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];   
  8. //新增抄送 
  9.     NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];     
  10.     [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@
    ","]];   
  11. //新增密送 
  12.     NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];     
  13.     [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];   
  14. //新增主題 
  15.     [mailUrl appendString:@"&subject=my email"];   
  16. //新增郵件內容 
  17.     [mailUrl appendString:@
    "&body=<b>email</b> body!"];   
  18.     NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];     
  19.     [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];     
  20. }  

2.MFMailComposeViewController

MFMailComposeViewController是在IOS3.0新增的一個介面,它在MessageUI.framework中。通過呼叫MFMailComposeViewController,可以把郵件傳送視窗整合到我們的應用裡,傳送郵件就不需要退出程式了。MFMailComposeViewController的使用方法:
  • 1.專案中引入MessageUI.framework;
  • 2.在使用的檔案中匯入MFMailComposeViewController.h標頭檔案;
  • 3.實現MFMailComposeViewControllerDelegate,處理郵件傳送事件;
  • 4.調出郵件傳送視窗前先使用MFMailComposeViewController裡的“+ (BOOL)canSendMail”方法檢查使用者是否設定了郵件賬戶;
  • 5.初始化MFMailComposeViewController,構造郵件體
  1. // 
  2. //  ViewController.h 
  3. //  MailDemo 
  4. // 
  5. //  Created by LUOYL on 12-4-4. 
  6. //  Copyright (c) 2012年 http://luoyl.info. All rights reserved. 
  7. // 
  8. #import <UIKit/UIKit.h> 
  9. #import <MessageUI/MFMailComposeViewController.h> 
  10. @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>   
  11. @end  
  1. #pragma mark - 在應用內傳送郵件 
  2. //啟用郵件功能 
  3. - (void)sendMailInApp   
  4. {   
  5.     Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));    
  6. if (!mailClass) {   
  7.         [self alertWithMessage:@"當前系統版本不支援應用內傳送郵件功能,您可以使用mailto方法代替"];   
  8. return;   
  9.     }   
  10. if (![mailClass canSendMail]) {   
  11.         [self alertWithMessage:@"使用者沒有設定郵件賬戶"];   
  12. return;   
  13.     }   
  14.     [self displayMailPicker];   
  15. }   
  16. //調出郵件傳送視窗 
  17. - (void)displayMailPicker   
  18. {   
  19.     MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];     
  20.     mailPicker.mailComposeDelegate = self;     
  21. //設定主題   
  22.     [mailPicker setSubject: @"eMail主題"];     
  23. //新增收件人 
  24.     NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"];   
  25.     [mailPicker setToRecipients: toRecipients];     
  26. //新增抄送 
  27.     NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];     
  28.     [mailPicker setCcRecipients:ccRecipients];         
  29. //新增密送 
  30.     NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];     
  31.     [mailPicker setBccRecipients:bccRecipients];     
  32. // 新增一張圖片   
  33.     UIImage *addPic = [UIImage imageNamed: @"[email protected]"];     
  34.     NSData *imageData = UIImagePNGRepresentation(addPic);            // png      
  35. //關於mimeType:http://www.iana.org/assignments/media-types/index.html 
  36.     [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];     
  37. //新增一個pdf附件 
  38.     NSString *file = [self fullBundlePathFromRelativePath:@"高質量C++程式設計指南.pdf"];   
  39.     NSData *pdf = [NSData dataWithContentsOfFile:file];   
  40.     [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高質量C++程式設計指南.pdf"];     
  41.     NSString *emailBody = @"<font color='red'>eMail</font> 正文";     
  42.     [mailPicker setMessageBody:emailBody isHTML:YES];     
  43.     [self presentModalViewController: mailPicker animated:YES];     
  44.     [mailPicker release];     
  45. }   
  46. #pragma mark - 實現 MFMailComposeViewControllerDelegate  
  47. - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error   
  48. {   
  49. //關閉郵件傳送視窗 
  50.     [self dismissModalViewControllerAnimated:YES];   
  51.     NSString *msg;     
  52. switch (result) {     
  53. case MFMailComposeResultCancelled:     
  54.             msg = @"使用者取消編輯郵件";     
  55. break;     
  56. case MFMailComposeResultSaved:     
  57.             msg = @"使用者成功儲存郵件";     
  58. break;     
  59. case MFMailComposeResultSent:     
  60.             msg = @"使用者點擊發送,將郵件放到佇列中,還沒傳送";     
  61. break;     
  62. case MFMailComposeResultFailed:     
  63.             msg = @"使用者試圖儲存或者傳送郵件失敗";     
  64. break;     
  65. default:     
  66.             msg = @"";   
  67. break;     
  68.     }     
  69.     [self alertWithMessage:msg];   
  70. }   

相關推薦

IOSEmail方法

IOS系統框架提供的兩種傳送Email的方法:openURL 和 MFMailComposeViewController。藉助這兩個方法,我們可以輕鬆的在應用里加入如使用者反饋這類需要傳送郵件的功能。 1.openURL 使用openURL呼叫系統郵箱客戶端是我們在

Ubuntu開機啟動的方法總結

一.第一種方法 1,新建個指令碼檔案new_service.sh #!/bin/bash # command content exit 0 2,設定許可權 sudo chmod 755 new_service.sh 3,把指令碼放置到啟動目錄下 sudo mv new_servi

SA:T1編寫主函數法和T2Matlab的SA工具箱GUI法,方法實現對二元函數優化求解——Jason niu

lin plot itl 最優解 IT 主函數 alt 圖片 gui %SA:T1法利用Matlab編寫主函數實現對定義域[-5,5]上的二元函數求最優解—Jason niu [x,y] = meshgrid(-5:0.1:5,-5:0.1:5); z = x.^2 +

android中RadioButton中的的圓圈如何去掉,方法,一.xml、一程式碼中

第一種.xml中 我想大家都知道 android:button="@null"其實這就是簡單的多RadioButton做的一個自定義,null就是給他為空,所以就變相的隱藏掉了它自帶那個圓圈。 重點是第二種的方法,在java程式碼中:RadioButton IB_PullD

深入講解iOS鍵盤三:定義鍵盤的方法

iOS系統提供了多種鍵盤,我們可以通過Enum型別設定。但有的時候由於某些特殊業務的需要,我們不得不自定義鍵盤,比如某些銀行的APP處於安全考慮,他們鍵盤數字的位置是隨機的,這個時候只能自定義鍵盤。幸運的是,iOS也為我們提供了多種方式自定義鍵盤。我們可以根據自身情況選擇合適

使用PHP生成二維碼的方法(logo圖像)

好的 區域 chl sta n) 方便 ram 尺寸 混合 一、利用Google API生成二維碼 Google提供了較為完善的二維碼生成接口,調用API接口很簡單,以下是調用代碼: $urlToEncode="http://www.jb51.net"; generat

輸入空格字符串的方法

現在 a10 lin ould char 錯誤 會有 遇到 意思 這是我們平常用的:   char s[100];   scanf("%s",s);//cin>>s;   輸入字符串時,當遇到空格就自動停止輸入,導致空格後門的字符沒有按我們設想的輸入。

MyBatis無限級分類實現的方法--關聯與map集合

except app exce utf-8 elf findall ldr ati tex 1、這回先創建數據庫吧 下表cid是CategoryId的縮寫,cname是CategoryName的縮寫,pid是parentId的縮寫 無限級分類一般都包含這三個屬性,至少也要包

學習windows編程 day3 之 定義畫筆的方法

cas delete tro HP rec col 編程 UC eat LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; P

PHP生成logo圖像二維碼的方法

width 調用 height word api 生成 table 中間 log 本文主要和大家分享PHP生成帶logo圖像二維碼的兩種方法,主要以文字和代碼的形式和大家分享,希望能幫助到大家。 一、利用Google API生成二維碼Google提供了較為完善的二維碼生

Email方法

IOS系統框架提供的兩種傳送Email的方法:openURL 和 MFMailComposeViewController。藉助這兩個方法,我們可以輕鬆的在應用里加入如使用者反饋這類需要傳送郵件的功能。   1.openURL 使用openURL呼叫系統郵

CSS實現適應不同大小螢幕的背景大圖的方法(轉簡書)

CSS實現自適應不同大小螢幕的背景大圖的兩種方法 一張清晰漂亮的背景圖片能給網頁加分不少,設計師也經常會給頁面的背景使用大圖,我們既不想圖片因為不同解析度圖片變形,也不希望當在大屏的情況下,背景有一塊露白,簡而言之,就是實現能自適應螢幕大小又不會變形的背景大圖,而且背景圖片不會隨著

在TensorFlow中定義梯度的方法

前言 在深度學習中,有時候我們需要對某些節點的梯度進行一些定製,特別是該節點操作不可導(比如階梯除法如 10 /

echart去除網格線的方法和echart圖適應

1. yAxis : [                 {                   &nb

ionic2 從子頁面返回引數的方法

1、使用 Modal 代替 NavController 的 push 方法,然後在子頁面通過 dismiss 方法關閉時可攜帶引數返回母頁面。 在母頁面中: getDetailNews(id) { let newsModal = this.modalCtrl.cr

css實現欄佈局,左側固定寬,右側適應的七方法

一個面試會問的問題,如何實現兩個盒子,左側固定寬度,右側自適應。 下面是實現的其中方法: 1、利用 calc 計算寬度的方法 css程式碼如下: .box{overflow: hidden;height: 100px;margin: 10px 0;} .box&

docker容器定義映象的方法

目錄 1.使用docker commit 使用映象啟動容器,在該容器上修改,在使用命令另存為一個映象 實現思路:使用一個基礎的映象,這個映象可以在centos的官網進行下載,在使用docker進行建立 一個容器,進入到該容器中,刪除原有的yum

iOS方法刪除NSUserDefaults所有記錄

//方法一 NSString *appDomain = [[NSBundlemainBundle] bundleIdentifier];[[NSUserDefaults standardUser

定義具有跑馬燈效果的方法

普通的TextView可以實現跑馬燈,但是隻有當焦點在它上面時才有效。如何做一個自動的跑馬燈呢?第一種:繼承TextView,然後重寫isFocused()方法就可以了,簡單!Java程式碼 import android.content.Context; import

robotframework使用python定義“關鍵字”的方法:匯入庫(LIB)和匯入模組(py檔案)

1、匯入庫,需要把檔案做成包的形式 常見放置在,python主目錄的  \Lib\site-packages下 __init__.py 好處是:適合大規模的開發,包有多人負責,分模組開發,無限擴充套件檔案數量 缺點是:統一歸檔相對麻煩 2、匯入檔案,直