iOS5编程--官方例子代码研究--6.MailComposer

本文档版权归NickTang所有,没有本人书面或电子邮件允许,不许转载,摘录,发表。多谢!

我们很多时候需要在iOS程序中内置邮件发送功能,而这个例子基本上提供了我们所需要的所有功能。
注意一下几点
1.这个程序必须在设备上运行,关于如何在设备上运行程序,不是本文需要讲的,我会在以后的文章中讲述。
2.你的设备必须配置好一个邮件帐户,不然你没法看到好的效果。
3.关于如何发送多个附件,我会在最后提到。
4.基本的代码不会再分析,基础部分请看我前面的文章。
5.在你自己建立的工程中,比如加入后面这个framework:MessageUI.framework
分析代码如下:
1.

-(IBAction)showPicker:(id)sender

{

// This sample can run on devices running iPhone OS 2.0 or later  

// The MFMailComposeViewController class is only available in iPhone OS 3.0 or later. 

// So, we must verify the existence of the above class and provide a workaround for devices running 

// earlier versions of the iPhone OS. 

// We display an email composition interface if MFMailComposeViewController exists and the device can send emails.

// We launch the Mail application on the device, otherwise.

//上面的解释的非常清楚,关于这些类和api在那个版本中引入,必须加以关注。

//下面是一个动态运行期查询类名的一个很好的例子,不解释了,因为在前面的文章有解释到。

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

if (mailClass != nil)

{

// We must always check whether the current device is configured for sending emails

if ([mailClass canSendMail])

{

[self displayComposerSheet];

}

else

{

[self launchMailAppOnDevice];

}

}

else

{

[self launchMailAppOnDevice];

}

}

所以重点关注函数displayComposerSheet

-(void)displayComposerSheet 

{

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

picker.mailComposeDelegate = self;

[picker setSubject:@"Hello from California!"];


// Set up recipients

NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"]; 

NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; 

NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

[picker setToRecipients:toRecipients];

[picker setCcRecipients:ccRecipients];

[picker setBccRecipients:bccRecipients];

// Attach an image to the email

NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];

    NSData *myData = [NSData dataWithContentsOfFile:path];

[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];

//代码非常清楚,我也不加以解释了,注意下面这个注释掉的地方,很多人问如何发送多个附件,就是下面演示的这样就可以多加一个附件,

//既多次调用addAttachmentData:fileName:

/*path = [[NSBundle mainBundle] pathForResource:@"circle" ofType:@"png"];

    myData = [NSData dataWithContentsOfFile:path];

[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"circle"];*/

// Fill out the email body text

NSString *emailBody = @"It is raining in sunny California!";

[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];

    [picker release];

}


在点击发送后,协议

MFMailComposeViewControllerDelegate

的下面这个函数会被调用。

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 

{

message.hidden = NO;

// Notifies users about errors associated with the interface

switch (result)

{

case MFMailComposeResultCancelled:

message.text = @"Result: canceled";

break;

case MFMailComposeResultSaved:

message.text = @"Result: saved";

break;

case MFMailComposeResultSent:

message.text = @"Result: sent";

break;

case MFMailComposeResultFailed:

message.text = @"Result: failed";

break;

default:

message.text = @"Result: not sent";

break;

}

[self dismissModalViewControllerAnimated:YES];

}


上面的这个代码也是很简单的,所以也不再解释。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值