`

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];

}


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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics