문제

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:

enter image description here

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:

enter image description here

도움이 되었습니까?

해결책

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 (e.g. "application/zip") and file name (e.g. "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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top