標籤:
iPhone API已經提供了系統寫郵件介面的介面,使用MFMailComposeViewController,用來顯示介面. 項目中需要添加MessageUi.framework。標頭檔加入MFMailComposeViewControllerDelegate。#import sendMailViewController.m檔案的實現: - (void)viewDidLoad { UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; button.frame = CGRectMake(0, 40, 320, 50); [button setTitle: @"Mail" forState: UIControlStateNormal]; [button addTarget: self action: @selector(sendEMail) forControlEvents: UIControlEventTouchUpInside]; [self.view addSubview: button]; } - (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_ message:msg delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alert show]; [alert release]; } //點擊按鈕後,觸發這個方法 -(void)sendEMail { Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); if (mailClass != nil) { if ([mailClass canSendMail]) { [self displayComposerSheet]; } else { [self launchMailAppOnDevice]; } } else { [self launchMailAppOnDevice]; } } //可以發送郵件的話 -(void)displayComposerSheet { MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init]; mailPicker.mailComposeDelegate = self; //設定主題 [mailPicker setSubject: @"eMail主題"]; // 添加寄件者 NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"]; //NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; //NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]", nil]; [mailPicker setToRecipients: toRecipients]; //[picker setCcRecipients:ccRecipients]; //[picker setBccRecipients:bccRecipients]; // 添加圖片 UIImage *addPic = [UIImage imageNamed: @"123.jpg"]; NSData *imageData = UIImagePNGRepresentation(addPic); // png // NSData *imageData = UIImageJPEGRepresentation(addPic, 1); // jpeg [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"]; NSString *emailBody = @"eMail 本文"; [mailPicker setMessageBody:emailBody isHTML:YES]; [self presentModalViewController: mailPicker animated:YES]; [mailPicker release]; } -(void)launchMailAppOnDevice { NSString *recipients = @"mailto:[email protected]&subject=my email!"; //@"mailto:[email protected][email protected],[email protected]&subject=my email!"; NSString *body = @"&body=email body!"; NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]]; } - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { NSString *msg; switch (result) { case MFMailComposeResultCancelled: msg = @"郵件發送取消"; break; case MFMailComposeResultSaved: msg = @"郵件儲存成功"; [self alertWithTitle:nil msg:msg]; break; case MFMailComposeResultSent: msg = @"郵件發送成功"; [self alertWithTitle:nil msg:msg]; break; case MFMailComposeResultFailed: msg = @"郵件發送失敗"; [self alertWithTitle:nil msg:msg]; break; default: break; } [self dismissModalViewControllerAnimated:YES];
MFMailComposeViewController發送郵件的執行個體