【问题标题】:how to attach to an email a PDF file in Xcode5如何在 Xcode5 中将 PDF 文件附加到电子邮件中
【发布时间】:2014-07-01 18:47:09
【问题描述】:

我正在 Xcode5 中为 Ipad (iOS7.1) 开发一个应用程序,它打开一个电子邮件模式视图控制器,用于使用 MFMailComposeViewController 发送电子邮件

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

函数.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];
}

这是我的邮件截图:

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

任何帮助我都会感激不尽


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

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

这是我的结果:

【问题讨论】:

    标签: objective-c ipad email ios7.1


    【解决方案1】:

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

    使用该方法,您将能够为要发送的任何存档添加原始 NSData。只需确保您设置了适当的 mime 类型(例如“application/zip”)和文件名(例如“MyArchive.zip”)。

    对于 PDF 文件,the mime type would be "application/pdf",您可以使用类似“google.pdf”的文件名。然后你需要做的就是加载一个带有你想要附加的文件数据的 NSData 对象。

    【讨论】:

    • 我将这些行添加到我的电子邮件中,我在签名后看到一个小徽标,但是当我发送它时,我看不到我的 pdf 附件 NSData *pdfData = [NSData dataWithContentsOfFile:@"google. pdf"]; [mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"google.pdf"];
    • 是的,那是我的 pdf,是的,它在我的资源包中
    • 我怀疑您的问题是“pdfData”为零,因为您没有指定“google.pdf”文件的正确位置。尝试使用“NSData *pdfData = [NSData dataWithContentsOfURL:[[[NSBundle mainBundle] resourceURL] URLByAppendingPathComponent:@"google.pdf"]];”,看看效果是否更好。如果它不起作用,请在代码中设置断点并确保数据对象不为零并且其中包含多个字节。
    • 我也有同样的问题,但是这个例子对我不起作用
    猜你喜欢
    • 2017-11-07
    • 2016-06-13
    • 2023-03-07
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2014-05-12
    相关资源
    最近更新 更多