如何在Xcode5中将电子邮件附加到电子邮件中

时间:2014-05-13 22:03:12

标签: objective-c ipad email ios7.1

我在Xcode5中为Ipad(iOS7.1)开发了一个应用程序,它打开了一个电子邮件模式viewcontroller,用于通过MFMailComposeViewController发送电子邮件

我想将PDF档案附加到我的电子邮箱中 这是我的代码:

Functions.m:

#import <MessageUI/MessageUI.h>

@interface Functions()<MFMailComposeViewControllerDelegate>

@end

@implementation Functions

-(void)sendEmailInViewController:(UIViewController *)viewController {

    NSString *emailTitle = @"Hello";
    NSArray *toRecipents = [[NSArray alloc]initWithObjects:@"jesus@mymail.com", nil];
    NSMutableString *messageBody =[[NSMutableString alloc]init];

    [messageBody appendString:@"<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p><span style=\"font-size:14px;\"><span style=\"color: rgb(0, 0, 102);\"><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Jesus</strong><br />Gerente Select&nbsp;&nbsp; / Sucursal&nbsp;&nbsp;&nbsp;Santa Fe<br />Paseo de la Lidia No. 801&nbsp;Piso 8 Mod. 162<br />Col. Lomas del Pedregal, C.P. 01292, M&eacute;xico D.F.<br />Tel: + (55) 8728-0908. Ext. 12832<br />e-mail:&nbsp; jesus@mymail.com</span></span></span></p><p><span style=\"color:#000066;\"><span style=\"font-family: arial,helvetica,sans-serif;\"></span></span></p>"];



    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil) {
        MFMailComposeViewController * mailView = [[MFMailComposeViewController alloc] init];
        mailView.mailComposeDelegate = self;

        //Set the subject
        [mailView setSubject:emailTitle];

        //Set the mail body
        [mailView setMessageBody:messageBody isHTML:YES];
        [mailView setToRecipients:toRecipents];



        NSData *pdfData = [NSData dataWithContentsOfFile:@"google.pdf"];
        [mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"google.pdf"];


        //Display Email Composer
        if([mailClass canSendMail]) {
            [viewController presentViewController:mailView animated:YES completion:NULL];
        }
    }
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    switch (result){
        case MFMailComposeResultCancelled:NSLog(@"Mail cancelled"); break;
        case MFMailComposeResultSaved:    NSLog(@"Mail saved");     break;
        case MFMailComposeResultSent:     NSLog(@"Mail sent");      break;
        case MFMailComposeResultFailed:   NSLog(@"Mail sent failure: %@", [error localizedDescription]);                                 break;
        default:                                                    break;
    }

    // Close the Mail Interface
    if (!app) { app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; }
    if (!currentSplitViewController) {
        currentSplitViewController  = (UISplitViewController *) app.window.rootViewController;
    }

    navController        = [currentSplitViewController.viewControllers lastObject];

    [[navController topViewController] dismissViewControllerAnimated:YES completion:NULL];

}

@end

在任何viewController上我用这种方式调用我的方法:

- (IBAction)showEmail:(id)sender {
    [functions sendEmailInViewController:self];
}

这是我邮件的截图:

enter image description here

它显示了我的pdf文件的徽标,但是当我的邮件被发送时,我打开我的收件箱,但我只是找到了签名,但是有任何附件可供查看...如何正确附加和发送?

任何帮助我都会感激


编辑:这就是答案:我只需要添加这个

NSData *pdfData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"google" ofType:@"pdf"]];
        [mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"google"];

这是我的结果:

enter image description here

1 个答案:

答案 0 :(得分:4)

听起来你需要查看MFMailComposeViewController的"addAttachmentData:mimeType:fileName:"方法。

使用该方法,您可以为要发送的任何存档添加原始NSData。只需确定您设置了适当的mime类型(例如&#34; application/zip&#34;)和文件名(例如&#34; MyArchive.zip&#34;)。

对于PDF文件the mime type would be "application/pdf",您可以使用&#34; google.pdf&#34;等文件名。然后,您需要做的就是使用要附加的文件数据加载NSData对象。