溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

IOS自帶Email的兩種方法實例詳解

發(fā)布時間:2020-10-05 21:09:24 來源:腳本之家 閱讀:164 作者:lqh 欄目:移動開發(fā)

IOS自帶Email的兩種方法實例詳解

IOS系統(tǒng)框架提供的兩種發(fā)送Email的方法:openURL 和 MFMailComposeViewController。借助這兩個方法,我們可以輕松的在應(yīng)用里加入如用戶反饋這類需要發(fā)送郵件的功能。 

1.openURL

使用openURL調(diào)用系統(tǒng)郵箱客戶端是我們在IOS3.0以下實現(xiàn)發(fā)郵件功能的主要手段。我們可以通過設(shè)置url里的相關(guān)參數(shù)來指定郵件的內(nèi)容,不過其缺點很明顯,這樣的過程會導(dǎo)致程序暫時退出。下面是使用openURL來發(fā)郵件的一個小例子:
#pragma mark - 使用系統(tǒng)郵件客戶端發(fā)送郵件  

-(void)launchMailApp  
{   
  NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];  
  //添加收件人  
  NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];  
  [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];  
  //添加抄送  
  NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];   
  [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];  
  //添加密送  
  NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];   
  [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];  
  //添加主題  
  [mailUrl appendString:@"&subject=my email"];  
  //添加郵件內(nèi)容  
  [mailUrl appendString:@"&body=<b>email</b> body!"];  
  NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];   
  [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];   
} 
 

2.MFMailComposeViewController

MFMailComposeViewController是在IOS3.0新增的一個接口,它在MessageUI.framework中。通過調(diào)用

MFMailComposeViewController,可以把郵件發(fā)送窗口集成到我們的應(yīng)用里,發(fā)送郵件就不需要退出程序了。

MFMailComposeViewController的使用方法:

1.項目中引入MessageUI.framework;
2.在使用的文件中導(dǎo)入MFMailComposeViewController.h頭文件;
3.實現(xiàn)MFMailComposeViewControllerDelegate,處理郵件發(fā)送事件;
4.調(diào)出郵件發(fā)送窗口前先使用MFMailComposeViewController里的“+ (BOOL)canSendMail”方法檢查用戶是否設(shè)置了郵件賬戶;
5.初始化MFMailComposeViewController,構(gòu)造郵件體 

//  
// ViewController.h  
// MailDemo  
//  
// Created by LUOYL on 12-4-4.  
// Copyright (c) 2012年 http://luoyl.info. All rights reserved.  
//  
 
#import <UIKit/UIKit.h>  
#import <MessageUI/MFMailComposeViewController.h>  
 
@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>  
 
@end 

#pragma mark - 在應(yīng)用內(nèi)發(fā)送郵件  
//激活郵件功能  
- (void)sendMailInApp  
{  
  Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));  
  if (!mailClass) {  
    [self alertWithMessage:@"當前系統(tǒng)版本不支持應(yīng)用內(nèi)發(fā)送郵件功能,您可以使用mailto方法代替"];  
    return;  
  }  
  if (![mailClass canSendMail]) {  
    [self alertWithMessage:@"用戶沒有設(shè)置郵件賬戶"];  
    return;  
  }  
  [self displayMailPicker];  
}  
 
//調(diào)出郵件發(fā)送窗口  
- (void)displayMailPicker  
{  
  MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];   
  mailPicker.mailComposeDelegate = self;   
    
  //設(shè)置主題   
  [mailPicker setSubject: @"eMail主題"];   
  //添加收件人  
  NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];  
  [mailPicker setToRecipients: toRecipients];   
  //添加抄送  
  NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];   
  [mailPicker setCcRecipients:ccRecipients];     
  //添加密送  
  NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];   
  [mailPicker setBccRecipients:bccRecipients];   
    
  // 添加一張圖片   
  UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];   
  NSData *imageData = UIImagePNGRepresentation(addPic);      // png    
  //關(guān)于mimeType:http://www.iana.org/assignments/media-types/index.html  
  [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];   
  
  //添加一個pdf附件  
  NSString *file = [self fullBundlePathFromRelativePath:@"高質(zhì)量C++編程指南.pdf"];  
  NSData *pdf = [NSData dataWithContentsOfFile:file];  
  [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高質(zhì)量C++編程指南.pdf"];   
 
  NSString *emailBody = @"<font color='red'>eMail</font> 正文";   
  [mailPicker setMessageBody:emailBody isHTML:YES];   
  [self presentModalViewController: mailPicker animated:YES];   
  [mailPicker release];   
}  
 
#pragma mark - 實現(xiàn) MFMailComposeViewControllerDelegate  
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error  
{  
  //關(guān)閉郵件發(fā)送窗口  
  [self dismissModalViewControllerAnimated:YES];  
  NSString *msg;   
  switch (result) {   
    case MFMailComposeResultCancelled:   
      msg = @"用戶取消編輯郵件";   
      break;   
    case MFMailComposeResultSaved:   
      msg = @"用戶成功保存郵件";   
      break;   
    case MFMailComposeResultSent:   
      msg = @"用戶點擊發(fā)送,將郵件放到隊列中,還沒發(fā)送";   
      break;   
    case MFMailComposeResultFailed:   
      msg = @"用戶試圖保存或者發(fā)送郵件失敗";   
      break;   
    default:   
      msg = @"";  
      break;   
  }   
  [self alertWithMessage:msg];  
}  

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI