欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS自带Email的两种方法实例详解

程序员文章站 2023-12-19 13:53:52
ios自带email的两种方法实例详解 ios系统框架提供的两种发送email的方法:openurl 和 mfmailcomposeviewcontroller。借助这两...

ios自带email的两种方法实例详解

ios系统框架提供的两种发送email的方法:openurl 和 mfmailcomposeviewcontroller。借助这两个方法,我们可以轻松的在应用里加入如用户反馈这类需要发送邮件的功能。 

1.openurl

使用openurl调用系统邮箱客户端是我们在ios3.0以下实现发邮件功能的主要手段。我们可以通过设置url里的相关参数来指定邮件的内容,不过其缺点很明显,这样的过程会导致程序暂时退出。下面是使用openurl来发邮件的一个小例子:
#pragma mark - 使用系统邮件客户端发送邮件  

-(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"];  
  //添加邮件内容  
  [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中。通过调用

mfmailcomposeviewcontroller,可以把邮件发送窗口集成到我们的应用里,发送邮件就不需要退出程序了。

mfmailcomposeviewcontroller的使用方法:

1.项目中引入messageui.framework;
2.在使用的文件中导入mfmailcomposeviewcontroller.h头文件;
3.实现mfmailcomposeviewcontrollerdelegate,处理邮件发送事件;
4.调出邮件发送窗口前先使用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 - 在应用内发送邮件  
//激活邮件功能  
- (void)sendmailinapp  
{  
  class mailclass = (nsclassfromstring(@"mfmailcomposeviewcontroller"));  
  if (!mailclass) {  
    [self alertwithmessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"];  
    return;  
  }  
  if (![mailclass cansendmail]) {  
    [self alertwithmessage:@"用户没有设置邮件账户"];  
    return;  
  }  
  [self displaymailpicker];  
}  
 
//调出邮件发送窗口  
- (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:@"高质量c++编程指南.pdf"];  
  nsdata *pdf = [nsdata datawithcontentsoffile:file];  
  [mailpicker addattachmentdata: pdf mimetype: @"" filename: @"高质量c++编程指南.pdf"];   
 
  nsstring *emailbody = @"<font color='red'>email</font> 正文";   
  [mailpicker setmessagebody:emailbody ishtml:yes];   
  [self presentmodalviewcontroller: mailpicker animated:yes];   
  [mailpicker release];   
}  
 
#pragma mark - 实现 mfmailcomposeviewcontrollerdelegate  
- (void)mailcomposecontroller:(mfmailcomposeviewcontroller *)controller didfinishwithresult:(mfmailcomposeresult)result error:(nserror *)error  
{  
  //关闭邮件发送窗口  
  [self dismissmodalviewcontrolleranimated:yes];  
  nsstring *msg;   
  switch (result) {   
    case mfmailcomposeresultcancelled:   
      msg = @"用户取消编辑邮件";   
      break;   
    case mfmailcomposeresultsaved:   
      msg = @"用户成功保存邮件";   
      break;   
    case mfmailcomposeresultsent:   
      msg = @"用户点击发送,将邮件放到队列中,还没发送";   
      break;   
    case mfmailcomposeresultfailed:   
      msg = @"用户试图保存或者发送邮件失败";   
      break;   
    default:   
      msg = @"";  
      break;   
  }   
  [self alertwithmessage:msg];  
}  

上一篇:

下一篇: