IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)
iOS系統(tǒng)框架提供的兩種發(fā)送Email的方法
1、使用openURL來實現(xiàn)發(fā)郵件的功能:
NSString *url = [NSString stringWithString: @"mailto:foo@example. com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"]; [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
缺點很明顯,這樣的過程會導(dǎo)致程序暫時退出,即使在iOS 4.x支持多任務(wù)的情況下,這樣的過程還是會讓人覺得不是很便。
2、使用MFMailComposeViewController來實現(xiàn)發(fā)郵件的功能,它在MessageUI.framework中,你需要在項目中加入該框架,并在使用的文件中導(dǎo)入MFMailComposeViewController.h頭文件。
#import <MessageUI/MFMailComposeViewController.h>; MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; controller.mailComposeDelegate = self; [controller setSubject:@"My Subject"]; [controller setMessageBody:@"Hello there." isHTML:NO]; [self presentModalViewController:controller animated:YES]; [controller release]; //使用該方法實現(xiàn)發(fā)送Email是最常規(guī)的方法,該方法有相應(yīng)的MFMailComposeViewControllerDelegate事件: - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error; { if (result == MFMailComposeResultSent) { NSLog(@"It's away!"); } [self dismissModalViewControllerAnimated:YES]; } //有一些相關(guān)的數(shù)據(jù)結(jié)構(gòu)的定義在頭文件中都有具體的描述: enum MFMailComposeResult { MFMailComposeResultCancelled,//用戶取消編輯郵件 MFMailComposeResultSaved,//用戶成功保存郵件 MFMailComposeResultSent,//用戶點擊發(fā)送,將郵件放到隊列中 MFMailComposeResultFailed//用戶試圖保存或者發(fā)送郵件失敗 }; typedef enum MFMailComposeResult MFMailComposeResult; // iOS3.0以上有效 //在頭文件中MFMailComposeViewController的部分方法順便提及: + (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); //如果用戶沒有設(shè)置郵件賬戶,則會返回NO,你可以用根據(jù)返回值來決定是使用MFMailComposeViewController 還是 mailto://的傳統(tǒng)方法,也或者,你可以選擇上文中提到的skpsmtpmessage來實現(xiàn)發(fā)送Email的功能。 - (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename; //NSData類型的attachment自然不必多說,關(guān)于mimeType需要一點說明,官方文檔里給出了一個鏈接http://www.iana.org/assignments/media-types/ ,這里列出的所有的類型都應(yīng)該支持。關(guān)于mimeType的用處,更多需要依靠搜索引擎了 =]
第二種方法的劣勢也很明顯,iOS系統(tǒng)替我們提供了一個mail中的UI,而我們卻完全無法對齊進行訂制,這會讓那些定制化成自己風(fēng)格的App望而卻步,因為這樣使用的話無疑太突兀了。
3、我們可以根據(jù)自己的UI設(shè)計需求來定制相應(yīng)的視圖以適應(yīng)整體的設(shè)計??梢允褂帽容^有名的開源SMTP協(xié)議來實現(xiàn)。
在SKPSMTPMessage類中,并沒有對視圖進行任何的要求,它提供的都是數(shù)據(jù)層級的處理,你之需要定義好相應(yīng)的發(fā)送要求就可以實現(xiàn)郵件發(fā)送了。至于是以什么樣的方式獲取這些信息,就可以根據(jù)軟件的需求來確定交互方式和視圖樣式了。
SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init]; testMsg.fromEmail = @"test@gmail.com"; testMsg.toEmail =@"to@gmail.com"; testMsg.relayHost = @"smtp.gmail.com"; testMsg.requiresAuth = YES; testMsg.login = @"test@gmail.com"; testMsg.pass = @"test"; testMsg.subject = [NSString stringWithCString:"測試" encoding:NSUTF8StringEncoding]; testMsg.bccEmail = @"bcc@gmail.com"; testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS! // Only do this for self-signed certs! // testMsg.validateSSLChain = NO; testMsg.delegate = self; NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, [NSString stringWithCString:"測試正文" encoding:NSUTF8StringEncoding],kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"]; NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath]; NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey, @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil]; testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil]; [testMsg send]; //該類也提供了相應(yīng)的Delegate方法來讓你更好的獲知發(fā)送的狀態(tài). -(void)messageSent:(SKPSMTPMessage *)message; -(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error;
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
iOS應(yīng)用開發(fā)中UIScrollView滾動視圖的基本用法總結(jié)
這篇文章主要介紹了iOS應(yīng)用開發(fā)中UIScrollView滾動視圖的基本用法總結(jié),作者還介紹了重寫UIScrollView中的hitTest方法來解決長按的事件問題,需要的朋友可以參考下2016-02-02

IOS 開發(fā)之?dāng)?shù)據(jù)存儲writeToFile的應(yīng)用實例

IOS給xcode工程關(guān)聯(lián)pod的實例詳解

iOS實現(xiàn)錄音轉(zhuǎn)碼MP3及轉(zhuǎn)碼BASE64上傳示例

實例講解iOS中的CATransition轉(zhuǎn)場動畫使用