从代码邮寄附件 PDF
我又来了,但遇到了一些麻烦。
我想从我的应用程序发送一封附有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这行代码绝对不应该编译:
除此之外,您的
filePath
可能是错误的。如果 pdf 作为附件正确加载,您应该不会看到附件的图标。相反,您会看到 pdf 本身的大图像。如果您在
NSData *pdfData =
之后设置断点并将鼠标悬停在 pdfData 上,它可能会显示它为 nil,数据为 0 字节。This line of code definitely should not compile:
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.