MFMailComposeViewController发送邮件

1.iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面.

2.项目中需要添加MessageUi.framework。头文件加入MFMailComposeViewControllerDelegate。#import <MessageUI/MessageUI.h>

 

- (void)viewDidLoad

{

  // 实例化按钮用来调用邮箱

  UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];

  button.frame = CGRectMake(0, 40, 320, 50);

  [button setTitle: @"Mail" forState: UIControlStateNormal];

  [button addTarget: self action: @selector(sendEmailAction) forControlEvents: UIControlEventTouchUpInside];

  [self.view addSubview: button];

 }

- (void)sendEmailAction

{

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

    if (mailClass != nil)

    {

        if ([mailClass canSendMail])

        {

            [self displayComposerSheet ];

        }else{

            [self launchMailAppOnDevice];

        }

    }else {

        [self launchMailAppOnDevice];

    }

}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result

{

// [self dismissModalViewControllerAnimated:YES];

    [self dismissViewControllerAnimated:YES completion:nil];

  NSString*msg;

  switch (result) {

  case MessageComposeResultCancelled:

    msg = @"发送取消";

    break;

  case MessageComposeResultSent:

    msg = @"发送成功";

    break;

  case MessageComposeResultFailed:

    msg = @"发送失败";

    break;

    default:

    break;

}

    UIAlertView*alert = [[UIAlertView alloc] initWithTitle:nil

                                                   message:msg

                                                  delegate:nil

                                         cancelButtonTitle:@"关闭"

                                         otherButtonTitles:nil];

    [alert show];

}

//email界面,界面添加于window上

-(void)displayComposerSheet

{

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

        [picker setAccessibilityLanguage:@"Chinese"];

    if (!picker) {

          // 在设备还没有添加邮件账户的时候mailViewController为空,下面的present view controller会导致程序崩溃,这里要作出判断

          NSLog(@"设备还没有添加邮件账户");

          return;

    }

      picker.mailComposeDelegate = self;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {

        UIView *barBgV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];

        barBgV.backgroundColor = [UIColor colorWithRed:55/255.0 green:60/255.0 blue:100/255.0 alpha:1.0];

        UIImage *barBgImg = [Tool convertViewToImage:barBgV];

        [[picker navigationBar] setBackgroundImage:barBgImg forBarMetrics:UIBarMetricsDefault];

    }

 

// Set up recipients

      NSArray *toRecipients = [NSArray arrayWithObject:staff.email];

 

      [picker setToRecipients:toRecipients];

    

//    NSString *path = [[NSBundle mainBundle] pathForResource:@"Icon@2x.png"

//                                                     ofType:nil

//                                                inDirectory:nil];

//        NSData *myData = [NSData dataWithContentsOfFile:path];

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

    [picker setMessageBody:nil isHTML:YES];

      [self presentViewController:picker animated:YES completion:^{

          picker.accessibilityElementsHidden = YES;

    }];

  // [self presentModalViewController:picker animated:YES];

   }

//跳转到系统email界面

-(void)launchMailAppOnDevice

{

    NSString *recipients = [NSString stringWithFormat:@"mailto:%@?&subject=",staff.email];

    

    NSString *email = recipients;

    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

}

#pragma mark - 代理

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

{

      //关闭邮件发送窗口

      [self dismissViewControllerAnimated:YES completion:nil];

      NSString *msg;

    switch (result)

    {

  case MFMailComposeResultCancelled:

            msg = @"用户取消编辑邮件";

    break;

  case MFMailComposeResultSaved:

            msg = @"用户成功保存邮件";

    break;

  case MFMailComposeResultSent:

              msg = @"发送成功";

      break;

  case MFMailComposeResultFailed:

            msg = @"用户试图保存或者发送邮件失败";

    break;

    default:

    break;

}

    [[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil]show];

 

}

 

设置中文:

1.可以在项目的list属性文件中设置Localization native development region的属性值  为:China;

2.可以在调用MFMailComposeViewController的xib中设置Localization(方法如下:找到对应xib文件,右击该文件选择Get Infoà [General选项卡下单击{add Localization}])如中文简体{zh_CN}

3.或者在自己手机:设置-->通用--->多语言环境-->语言--->简体中文;

General--->International-->Language--->简体中文;

转载于:https://www.cnblogs.com/ZGSmile/p/3868523.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值