UIDocumentInteractionController和PDF

时间:2014-05-26 16:19:48

标签: ios uidocumentinteraction

我可以成功打开png文件,但是当我尝试打开pdf时,预览会显示"可移植文档格式"没有显示内容。

    self.docInteractionController.UTI = @"com.adobe.pdf";

    if ([operation isEqualToString:@"open"])

        if (![self.docInteractionController presentPreviewAnimated:YES])
              NSLog(@"Failed to open document in Document Dir");

任何提示?

1 个答案:

答案 0 :(得分:3)

对于那些在此结束的人......正如RyanJM指出的那样,这似乎表明该文件实际上没有按预期保存或URL表明它的位置。另一种可能性是您没有打开本地URL。虽然UIDocumentInteractionController可以获取URL,但它必须以file://

开头

您可以在本地下载,然后使用以下内容打开:

// Build URLS
NSURL* url = [[NSURL alloc] initWithString:@"http://MYURLGOESHERE"];
NSURL* documentsUrl = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
NSURL* destinationUrl = [documentsUrl URLByAppendingPathComponent:@"temp.pdf"];

// Actually download
NSError* err = nil;
NSData* fileData = [[NSData alloc] initWithContentsOfURL:url options:NSDataReadingUncached error:&err];
if (!err && fileData && fileData.length && [fileData writeToURL:destinationUrl atomically:true])
{
    // Downloaded and saved, now present the document controller (store variable, or risk garbage collection!)
    UIDocumentInteractionController* document = [UIDocumentInteractionController interactionControllerWithURL:destinationUrl];
    document.delegate = self;
    [document presentPreviewAnimated:YES];
}