从代码邮寄附件 PDF

发布于 2024-11-15 05:41:22 字数 980 浏览 4 评论 0原文

我又来了,但遇到了一些麻烦。

我想从我的应用程序发送一封附有 pdf 的电子邮件,因此我执行了以下操作:

- (IBAction) sendMail:(UIButton *)sender {

    MFMailComposeViewController *controller1 = [[MFMailComposeViewController alloc] init];

    controller1.mailComposeDelegate = self;
    if ([MFMailComposeViewController canSendMail]) {
        [controller1 setSubject:@"Brochure"];
        if (sender.tag == 101) {
            NSString *filePath = [[NSBundle mainBundle] pathForResource: @"web link" ofType: @"pdf"];  
            NSData *pdfData = [NSData dataWithContentsOfURL:filePath options: error:
            [controller1 setMessageBody:@"Brochure File" isHTML:YES];
            [controller1 addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"brochure.pdf"];
        }

        [self presentModalViewController:controller1 animated:YES];
    }
    [controller1 release];
}

没有问题,没有错误,什么也没有。当我尝试从 iPad 发送邮件时,会出现一个带有“附件”文件的小图标,但当我收到电子邮件时,它是空的,没有附件,什么也没有。我有什么遗漏的吗?

here I am again, with some trouble.

I want to send an email from my app with a pdf attached, so I did the following:

- (IBAction) sendMail:(UIButton *)sender {

    MFMailComposeViewController *controller1 = [[MFMailComposeViewController alloc] init];

    controller1.mailComposeDelegate = self;
    if ([MFMailComposeViewController canSendMail]) {
        [controller1 setSubject:@"Brochure"];
        if (sender.tag == 101) {
            NSString *filePath = [[NSBundle mainBundle] pathForResource: @"web link" ofType: @"pdf"];  
            NSData *pdfData = [NSData dataWithContentsOfURL:filePath options: error:
            [controller1 setMessageBody:@"Brochure File" isHTML:YES];
            [controller1 addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"brochure.pdf"];
        }

        [self presentModalViewController:controller1 animated:YES];
    }
    [controller1 release];
}

there's no problem, no errors, no nothing. When I try to send it from my iPad, there's a small icon with the file "attached" but when I receive the email its empty, no attachment, no nothing. Is there something I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此生挚爱伱 2024-11-22 05:41:22

这行代码绝对不应该编译:

NSData *pdfData = [NSData dataWithContentsOfURL:filePath options: error:

除此之外,您的 filePath 可能是错误的。如果 pdf 作为附件正确加载,您应该不会看到附件的图标。相反,您会看到 pdf 本身的大图像。

如果您在 NSData *pdfData = 之后设置断点并将鼠标悬停在 pdfData 上,它可能会显示它为 nil,数据为 0 字节。

This line of code definitely should not compile:

NSData *pdfData = [NSData dataWithContentsOfURL:filePath options: error:

Besides that it is likely that your filePath is wrong. If the pdf is loaded as an attachment properly you should not see an icon for the attachment. Instead you will see a large image of the pdf itself.

If you set a break point right after NSData *pdfData = and hover over pdfData it will probably show you that it is nil with 0 bytes of data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文