欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年06月21日 17:09:27   投稿:lqh  
這篇文章主要介紹了IOS自帶Email的兩種方法實例詳解的相關資料,需要的朋友可以參考下

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

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

1.openURL

使用openURL調(diào)用系統(tǒng)郵箱客戶端是我們在IOS3.0以下實現(xiàn)發(fā)郵件功能的主要手段。我們可以通過設置url里的相關參數(shù)來指定郵件的內(nèi)容,不過其缺點很明顯,這樣的過程會導致程序暫時退出。下面是使用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ā)送窗口集成到我們的應用里,發(fā)送郵件就不需要退出程序了。

MFMailComposeViewController的使用方法:

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

//  
// 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 - 在應用內(nèi)發(fā)送郵件  
//激活郵件功能  
- (void)sendMailInApp  
{  
  Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));  
  if (!mailClass) {  
    [self alertWithMessage:@"當前系統(tǒng)版本不支持應用內(nèi)發(fā)送郵件功能,您可以使用mailto方法代替"];  
    return;  
  }  
  if (![mailClass canSendMail]) {  
    [self alertWithMessage:@"用戶沒有設置郵件賬戶"];  
    return;  
  }  
  [self displayMailPicker];  
}  
 
//調(diào)出郵件發(fā)送窗口  
- (void)displayMailPicker  
{  
  MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];   
  mailPicker.mailComposeDelegate = self;   
    
  //設置主題   
  [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    
  //關于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  
{  
  //關閉郵件發(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];  
}  

相關文章

  • 詳解iOS開發(fā)中的轉場動畫和組動畫以及UIView封裝動畫

    詳解iOS開發(fā)中的轉場動畫和組動畫以及UIView封裝動畫

    這篇文章主要介紹了iOS開發(fā)中的轉場動畫和組動畫以及UIView封裝動畫,主要用到了CAAnimation類和UIView類,需要的朋友可以參考下
    2015-11-11
  • iOS獲取本地音頻文件(屬性/信息)

    iOS獲取本地音頻文件(屬性/信息)

    這篇文章主要為大家詳細介紹了iOS獲取本地音頻文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • iOS中利用CAEmitterLayer實現(xiàn)粒子動畫詳解

    iOS中利用CAEmitterLayer實現(xiàn)粒子動畫詳解

    粒子效果應該對現(xiàn)在很多人來說并不陌生,我們之前也分享了一些相關文章,下面這篇文章主要給大家介紹了關于iOS中利用CAEmitterLayer實現(xiàn)粒子動畫的相關資料,文中介紹的非常詳細,需要的朋友們下面來一起看看吧。
    2017-06-06
  • iOS開發(fā)技巧之自定義相機

    iOS開發(fā)技巧之自定義相機

    這篇文章主要為大家詳細介紹了iOS開發(fā)技巧之自定義相機,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 解決Alamofire庫在iOS7下設置Head無效的問題

    解決Alamofire庫在iOS7下設置Head無效的問題

    本文主要介紹Alamofire庫在iOS下設置Head,這里通過代碼實例解決不同版本的IOS系統(tǒng)出現(xiàn)的問題,有需要的小伙伴可以參考下
    2016-07-07
  • IOS 中NSUserDefaults讀取和寫入自定義對象的實現(xiàn)方法

    IOS 中NSUserDefaults讀取和寫入自定義對象的實現(xiàn)方法

    這篇文章主要介紹了IOS 中NSUserDefaults讀取和寫入自定義對象的實現(xiàn)方法的相關資料,希望通過本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • iOS中視頻播放的基本方法總結

    iOS中視頻播放的基本方法總結

    這篇文章主要給大家介紹了關于iOS中視頻播放的基本方法的相關資料,文中分別介紹了MPMoviePlayerController、MPMoviePlayerViewController、AVPlayer以及AVPlayerViewController四種方法,需要的朋友可以參考下
    2018-07-07
  • iOS自定義身份證鍵盤

    iOS自定義身份證鍵盤

    這篇文章主要為大家詳細介紹了iOS自定義身份證鍵盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • IOS 單擊手勢的添加實現(xiàn)代碼

    IOS 單擊手勢的添加實現(xiàn)代碼

    這篇文章主要介紹了IOS 單擊手勢的添加實現(xiàn)代碼的相關資料,需要的朋友可以參考下
    2017-05-05
  • iOS 撥打電話代碼的三種方式

    iOS 撥打電話代碼的三種方式

    本文給大家分享三種方式實現(xiàn)ios撥打電話的代碼,非常不錯,具有一定的參考借鑒價值,感興趣的朋友一起看看吧
    2016-11-11

最新評論