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 = @"[email protected]&subject=!設備唯一識別號";
    //@"mailto:[email protected][email protected],[email protected]&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:@"[email protected]"];
    [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];
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章