0

我正在使用下面的代码从网站下载 pdf 文件,然后在 uiwebview 中显示它

NSString *url = [NSString stringWithString:[[[popOverContent currentValues]     objectAtIndex:0]objectForKey:@"Web"]];

    // Determile cache file path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,      NSUserDomainMask, YES);
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.pdf"];

    // Download and write to file
    NSURL *url2 = [NSURL URLWithString:url];
    NSData *urlData = [NSData dataWithContentsOfURL:url2];
    [urlData writeToFile:filePath atomically:YES];

    fileToAtatch = urlData;

    // Load file in UIWebView
    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

此时一切正常,但后来当我使用以下代码使用 mfmailcomposer 将 pdf 文件附加到电子邮件时,我遇到了问题。

-(IBAction)EmailPressed:(id)sender
{
if ([MFMailComposeViewController canSendMail]) {

    MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
    viewController.mailComposeDelegate = self;
    NSString *query = @"please find atatched the requested data sheet";
    [viewController setSubject:[popOverContent selectedSize]];
    [viewController setMessageBody:query isHTML:NO];
    [viewController addAttachmentData:fileToAtatch mimeType:@"application/pdf" fileName:[popOverContent selectedSize]];
    [self presentModalViewController:viewController animated:YES];

}

}

此代码完全按预期工作,多页 pdf 按预期附加。

如果我使用 ios 5.0.1 在 iPad 上测试应用程序,将电子邮件发送给自己,当我单击电子邮件中 ​​pdf 的图标时,pdf 不会打开,并且预览只是坐在那里,uiprogress 指示器在旋转。但是,如果我在带有 ios 6.0.1 的 ipad2 上以相同的方式测试应用程序,则 pdf 文件将毫无问题地打开。它也适用于 iphone 5 和带有山狮的 mac。

那么发生了什么?谁能告诉我我的代码和实现有什么问题,让我在 ipad 1 上出现这种奇怪的行为,但在 ipad 2 等上却没有。

请指教

谢谢

4

1 回答 1

1

我终于弄清楚了,并通过如下更改我的代码解决了这个问题

         fileName = [selectedSizeToUse stringByAppendingString:@".pdf"];
         [mailController setSubject:selectedSizeToUse];
         [mailController addAttachmentData:fileToAtatch mimeType:@"application/pdf" fileName:fileName];

似乎对于 ios5 实现,pdf 文件需要在预览应用程序中识别 .pdf 扩展名,而在 iOS 6 实现中,预览应用程序足够聪明,可以在没有 .pdf 扩展名的情况下显示 pdf。

希望这可以帮助其他陷入困境的人

于 2013-02-01T12:21:54.560 回答