本文档版权归NickTang所有,没有本人书面或电子邮件允许,不许转载,摘录,发表。多谢!
-(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];
}
上面的这个代码也是很简单的,所以也不再解释。