【问题标题】:Attach a pdf file in-app-mail在应用程序邮件中附加 pdf 文件
【发布时间】:2012-02-01 18:33:26
【问题描述】:

我尝试将我的 pdf 附加到应用程序内邮件。应用程序内邮件显示一个带有 pdf 的图标,但它不发送它。不知道为什么……

代码如下:

- (void)openInEmail {

    if ([MFMailComposeViewController canSendMail]) {

        MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
        viewController.mailComposeDelegate = self;

        [viewController setSubject:@"Stundenplan 1A"];

        NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
        NSString *docDirectory = [sysPaths objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/Stundenplan_1A.pdf", docDirectory];

        NSMutableData *data=[NSMutableData dataWithContentsOfFile:filePath];

        [viewController addAttachmentData:data mimeType:@"text/pdf" fileName:@"Stundenplan_1A.pdf"];

        [self presentModalViewController:viewController animated:YES];    }
}

有什么想法吗?

【问题讨论】:

  • 需要注意的是,PDF 的 mimeType 是 application/pdf,而不是 text/pdf。这可能是问题所在。
  • 您确定命名约定吗?我的意思是名称与 PDF 文件匹配吗?
  • 是的,我仔细检查了所有内容,我真的不知道为什么它不起作用

标签: iphone objective-c email ios5 xcode4.2


【解决方案1】:

你试过了吗?

NSString *filePath = [documentsDirectory stringByAppendingFileComponent:@"%@/Stundenplan_1A.pdf"];

而不是

NSString *filePath = [NSString stringWithFormat:@"%@/Stundenplan_1A.pdf", docDirectory];

你可以用 NSData 代替 NSMutableData

NSData *data = [NSData dataWithContentsOfFile:file];

【讨论】:

  • 我正要说使用 "NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Stundenplan_1A" ofType:@"pdf"];"但它返回的结果与他所拥有的完全相同。
【解决方案2】:

像这样更改 mime 类型。

NSURL *url = [NSURL URLWithString:pdfURL];
NSData *pdfData = [NSData dataWithContentsOfURL:url];
[mailComposeView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"CheckList.pdf"];

【讨论】:

  • 还是不行,如果我给自己发邮件,我会收到一封空邮件
  • 现在可以使用该代码: NSString *path = [[NSBundle mainBundle] pathForResource:@"Stundenplan_1A" ofType:@"pdf"]; NSData *myData = [NSData dataWithContentsOfFile: path]; [viewController addAttachmentData:myData mimeType:@"application/pdf" fileName:@"Stundenplan_1A.pdf"];
  • 是的,我也知道你的 pdf 位置。 NSLog 路径,可能是您没有正确获取路径。文件不包含那里。不管怎样,你解决了!!
猜你喜欢
  • 2023-03-07
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多