how to attach to an email a PDF file in Xcode5

I'm developing an app for Ipad (iOS7.1) in Xcode5 which opens an e-mail modal viewcontroller for sending email by using MFMailComposeViewController

I want to attach a PDF archive to my email this is my code:

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

on any viewController I call to my method this way:

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

this is the screenshot of my mail:

在这里输入图像描述

it shows a logo of my pdf file, but when my message is sent, I open my inbox but I just find the signature, but anything attached for viewing... how to attach and send it correctly???

any help I'll appreciate


EDIT: this is the answer: I just had to add this

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

this is my result:

在这里输入图像描述


Sounds like you need to look at the " addAttachmentData:mimeType:fileName: " method of MFMailComposeViewController.

And using that method, you'll be able to add the raw NSData for whatever archive you want to send along. Just make certain you set the appropriate mime type (eg " application/zip ") and file name (eg "MyArchive.zip").

For PDF files, the mime type would be " application/pdf " and you would use a filename like "google.pdf". Then all you would need to do is load a NSData object with the file data you want to attach.

链接地址: http://www.djcxy.com/p/8182.html

上一篇: 你需要在服务器上安装Adobe PDF以使用iTextSharp吗?

下一篇: 如何在Xcode5中附加到电子邮件PDF文件