Mail 邮件发送

1.首先添加 MessageUI.framework 框架

2. 引入框架

  在类的头部

  #import <MessageUI/MessageUI.h>

  #import <MessageUI/MFMailComposeViewController.h>

3. 实现接口 

  <MFMailComposeViewControllerDelegate>


- (void)handleDoubleTap:(UIGestureRecognizer *)doubleTap{

    model.isShowUDID = !model.isShowUDID;
    if (doubleTap.state == UIGestureRecognizerStateEnded) {
//        展示设备udid
        if (model.isShowUDID) {
            if (!label) {
                label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame) - 80, CGRectGetWidth(self.view.frame), 80)];
                label.numberOfLines = 0;
                label.font = [UIFont systemFontOfSize:15.f];
                label.textAlignment = NSTextAlignmentCenter;
                label.text = [ManagerCoca udid];
                label.backgroundColor = [UIColor clearColor];
                label.textColor = [UIColor whiteColor];
                [self.view addSubview:label];
            }
            label.hidden = NO;
            Class mailClass = NSClassFromString(@"MFMailComposeViewController");
            if (mailClass!=nil) {
                if ([mailClass canSendMail]) {
                    [self displayComposerSheet:[ManagerCoca udid]];
                }
            }else{
                [self launchMailAppOnDevice:[ManagerCoca udid]];
            }
        }else{
            label.hidden = YES;
        }
    }
}

- (void)launchMailAppOnDevice:(NSString *)udid{
    NSString *recipients = @"example@qq.com&subject=!设备唯一识别号";
    //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";
    NSString *emailBody = [NSString
                           stringWithFormat:@"&body=!Version: %@\nUniversallyUniqueIdentifier: %@\n\n",
                           [[UIDevice currentDevice] systemVersion],udid];
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, emailBody];
    email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
-(void)displayComposerSheet:(NSString *)udid

{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];/*MFMailComposeViewController邮件发送选择器*/
    picker.mailComposeDelegate = self;
    [picker setSubject:@"设备唯一识别号"];/*emailpicker标题主题行*/
    NSArray *toRecipients = [NSArray arrayWithObject:@"example@qq.com"];
    [picker setToRecipients:toRecipients];
    NSString *emailBody = [NSString                           
                           stringWithFormat:@"Version: %@\nUniversallyUniqueIdentifier: %@\n\n",
                           [[UIDevice currentDevice] systemVersion],udid];
    [picker setMessageBody:emailBody isHTML:NO];
    [self presentModalViewController:picker animated:YES];
    
}
- (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
                                                    message:msg
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
}
- (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];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值