iOS--发邮件--大山个人版
1.首先添加 MessageUI.framework 框架 2. 引入框架 在类的头部 #import <MessageUI/MessageUI.h> #import <MessageUI/MFMailComposeViewController.h> 3. 实现接口 <MFMailComposeViewControllerDelegate> 4. 当点击一个button 跳转到发邮件的页面 调用我们发邮件 发邮件是有两种方式 : 1. 当你的设备支持的时候 the current device is configured for sending emails 我们使用一下的tool methods 中的displayComposerSheet 方法来发送邮件(其中使用了apple 集成好的 邮件picker -- MFMailComposeViewController) 在这里 我们将这个picker 看做是一个 模式视图 ModalViewController 推出了 2. 当设备不支持的时候 我们采用 launchMailAppOnDevice 方法发送 ( 采用打开一个url地址的 方式来发) ok.. -----点击按钮出发的方法 - (IBAction)contactBtnPressed:(id)sender { 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 { [selflaunchMailAppOnDevice]; } } else { [selflaunchMailAppOnDevice]; } } ----- tool Methods 工具方法
// 1. Launches the Mail application on the device. -(void)launchMailAppOnDevice { NSString *recipients = @"mailto:[email protected]&subject=Pocket Truth or Dare Support"; NSString *body = @"&body=email body!"; NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:email]]; }
// 2. Displays an email composition interface inside the application. Populates all the Mail fields. -(void)displayComposerSheet { MFMailComposeViewController *picker = [[MFMailComposeViewControlleralloc] init];/*MFMailComposeViewController邮件发送选择器*/ picker.mailComposeDelegate = self; [picker setSubject:@"Pocket Truth or Dare Support"];/*emailpicker标题主题行*/
// Custom NavgationBar background And set the backgroup picture picker.navigationBar.tintColor = [UIColorcolorWithRed:209.0/255green:183.0/255blue:126.0/255alpha:1.0];
// picker.navigationBar.tintColor = [UIColor colorWithRed:178.0/255 green:173.0/255 blue:170.0/255 alpha:1.0];
if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 5.0) { [picker.navigationBarsetBackgroundImage:[UIImageimageNamed:@"nav_bg.png"]forBarMetrics:UIBarMetricsDefault]; }
// Set up recipients NSArray *toRecipients = [NSArrayarrayWithObject:@"[email protected]"];
[picker setToRecipients:toRecipients];
// Fill out the email body text struct utsname device_info;
uname(&device_info); NSString *emailBody = [NSString stringWithFormat:@"Model: %s\nVersion: %@\nApp: %@\nFeedback here:\n",device_info.machine, [[UIDevicecurrentDevice] systemVersion],/*设备系统环境*/ [[[NSBundlemainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];/**/
NSLog(@"ios 应用发布后 .app 应用文件路径::%@",[NSBundle mainBundle] ); NSLog(@"ios 应用发布后 .app 应用文件内 ::%@",[[NSBundle mainBundle] infoDictionary]);
[picker setMessageBody:emailBody isHTML:NO];
[selfpresentModalViewController:picker animated:YES]; [picker release]; }
// 3. 一个备用的方法 //- (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg //{ // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_ // message:msg // delegate:nil // cancelButtonTitle:@"Sure" // otherButtonTitles:nil]; // [alert show]; // [alert release]; //} ----协议的委托方法
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation. - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { // NSString *title = @"Mail"; // NSString *msg; // switch (result) // { // case MFMailComposeResultCancelled: // msg = @"Mail canceled";//@"邮件发送取消"; // break; // case MFMailComposeResultSaved: // msg = @"Mail saved";//@"邮件保存成功"; // [self alertWithTitle:title msg:msg]; // break; // case MFMailComposeResultSent: // msg = @"Mail sent";//@"邮件发送成功"; // [self alertWithTitle:title msg:msg]; // break; // case MFMailComposeResultFailed: // msg = @"Mail failed";//@"邮件发送失败"; // [self alertWithTitle:title msg:msg]; // break; // default: // msg = @"Mail not sent"; // [self alertWithTitle:title msg:msg]; // break; // } [self dismissModalViewControllerAnimated:YES]; }
|