简体   繁体   English

如何让 QuickLook 显示脱机文件?

[英]How do I get QuickLook to show offline files?

Our iPad app can show Documents and save them offline when needed.我们的 iPad 应用程序可以显示文档并在需要时离线保存它们。 I've got a QLPreviewController subclass named DocumentViewController (named DVC from now on) for showing them.我有一个名为 DocumentViewController(从现在起名为 DVC)的 QLPreviewController 子类来显示它们。

Workflow of the app: - The user clicks a name of a document and the DVC is pushed on to show the document.应用程序的工作流程: - 用户单击文档的名称,然后 DVC 被推送以显示文档。 - The DVC downloads the file offline and shows it when done. - DVC 离线下载文件并在完成后显示。 (So the HTTP URL is downloaded, stored offline, and an offline URL is returned) (因此下载了 HTTP URL ,离线存储,并返回离线 URL )

The weird thing is, is that only PDF files are working with the offline URL, and the rest crashes.. (it works with online links though)奇怪的是,只有 PDF 文件与离线 URL 和 rest 崩溃一起使用。(虽然它适用于在线链接)

I did some tests and when I put file:// before the offline link the app does not crash but the DVC is ging me some information about the file (like that it is a excel 97-2004 document).我做了一些测试,当我在脱机链接之前放置 file:// 时,应用程序不会崩溃,但 DVC 会向我提供有关该文件的一些信息(例如它是 excel 97-2004 文档)。

So some info is transferred, but I can't figure out what the problem is.所以一些信息被转移了,但我无法弄清楚问题是什么。

Here are some screenshots and after that some code.这是一些屏幕截图,然后是一些代码。

在此处输入图像描述

code: Note that Document is a model class with document properties like id, name, file type and url.代码:请注意,文档是 model class 文档属性,如 id、名称、文件类型和 url。

//DVC QLPreviewController dataSource method for returning url
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger)index
{
    [SaveHelper saveDocumentFileAndPropertyWithDocument:document];

    //[SaveHelper getDocumentFileWithDocument:document]; without file://
    //if I return document.documentUrl it is working with all files except iworks files)
    return [SaveHelper getDocumentFileAsPathWithDocument:document]; //with file://
}

//SaveHelper methods
+ (NSString *)documentFilePathWithDocument:(Document *)document
{
    return [[self documentFilePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%d.%@", DOCUMENT_FILE_PREFIX, document.documentId, document.documentType]];
}

+ (NSURL *)saveDocumentFileAndPropertyWithDocument:(Document *)document
{
    if([self saveDocumentPropertyWithDocument:document])
    {
        return [self saveDocumentFileWithDocument:document];
    }

    return nil;
}

+ (NSURL *)saveDocumentFileWithDocument:(Document *)document
{
    NSData *data = [NSData dataWithContentsOfURL:document.documentURL];

    NSString *fullPath = [self documentFilePathWithDocument:document];

   if([[NSKeyedArchiver archivedDataWithRootObject:data] writeToFile:fullPath atomically:YES])
   {
       return [NSURL fileURLWithPath:fullPath];
   }

    return nil;
}

+ (NSURL *)getDocumentFileWithDocument:(Document *)document
{
    return [NSURL fileURLWithPath:[self documentFilePathWithDocument:document]];
}

+ (NSURL *)getDocumentFileAsPathWithDocument:(Document *)document
{
    return [NSURL fileURLWithPath:[@"file://" stringByAppendingPathComponent:[[self getDocumentFileWithDocument:document] absoluteString]]];
}

If more code needed, just say.如果需要更多代码,请说。

EDIT:编辑:

When logging the URL passed trough the 'getDocumentFileAsPathWithDocument' method:记录 URL 通过“getDocumentFileAsPathWithDocument”方法传递时:

url: file:/var/mobile/Applications/xx-xx/Documents/documentFiles/file_20.pdf
url: file:/var/mobile/Applications/xx-xx/Documents/documentFiles/file_80.docx

Where the PDF file is working and the docx not PDF 文件在哪里工作,而 docx 不在

When I try to load an image(jpg) from local storage I get a black screen with this error message:当我尝试从本地存储加载图像(jpg)时,出现黑屏并显示以下错误消息:

warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.5 (8L1)/Symbols/System/Library/Frameworks/QuickLook.framework/DisplayBundles/Image.qldisplay/Image (file not found).
warning: No copy of Image.qldisplay/Image found locally, reading from memory on remote device.  This may slow down the debug session.

EDIT:编辑:

The webview does not work either with the local urls. webview 也不适用于本地 URL。 PDF is fine but the office files gives an message "Unable to read Document, the file format is invalid". PDF 没问题,但是office文件提示“无法读取文档,文件格式无效”。 The iWorks documents give the same error as the quicklook. iWorks 文档给出与 quicklook 相同的错误。 I think its somewhere at the save and load of the format, I savve them as a NSDATA but after that there is no hint for the iPad to see if it is for example a word document (only the extension).我认为它在格式的保存和加载的某个地方,我将它们保存为 NSDATA 但之后没有提示 iPad 查看它是否是例如 word 文档(仅扩展名)。

You haven't posted your download code, but I believe that the problem is there.你还没有发布你的下载代码,但我相信问题就在那里。 Files from Pages (.pages extension) aren't actual files, they are bundles , ie directories that contain files and show as a single item in Finder (look up a.pages file in Finder, right-click it and select 'Show contents'). Pages 中的文件(.pages 扩展名)不是实际文件,它们是bundles ,即包含文件并在 Finder 中显示为单个项目的目录(在 Finder 中查找 a.pages 文件,右键单击它并 select '显示内容')。 Downloading a.pages file is actually like downloading a directory: it depends on the web server what kind of result you get but it's most likely an error page.下载a.pages文件实际上就像下载一个目录:它取决于web服务器你得到什么样的结果但它很可能是一个错误页面。

You could detect that it's a.pages file and try to download all of its contents manually, but you'd have to study the structure of the files to see if that's possible because it's unlikely that you can request the contents of the directory from a web server.您可以检测到它是 a.pages 文件并尝试手动下载其所有内容,但您必须研究文件的结构以查看是否可行,因为您不太可能从web 服务器。

The results for the.ppt and.xls files look normal to me; .ppt 和.xls 文件的结果对我来说看起来很正常; I think it unlikely that the iPad can preview MS Office documents at all.我认为 iPad 根本不可能预览 MS Office 文档。

Edit: apologies, I just read that iOS can preview MS Office documents.编辑:抱歉,我刚刚读到 iOS可以预览 MS Office 文档。 Perhaps the documents get somehow corrupted during download?也许文件在下载过程中以某种方式损坏? Have you tried to set your download location to the app's documents folder and enable iTunes file sharing?您是否尝试将下载位置设置为应用程序的文档文件夹并启用 iTunes 文件共享? That way you can download some documents, pull them off your device and then try to open it on your PC to see if that works.这样您就可以下载一些文档,将它们从您的设备中取出,然后尝试在您的 PC 上打开它以查看是否有效。

We finally found the solution!我们终于找到了解决方案!

I was right that the problem was with saving the document.我是对的,问题在于保存文档。

I needed to change the save method in:我需要更改保存方法:

+ (NSURL *)saveDocumentFileWithDocument:(Document *)document
{
    NSData *data = [NSData dataWithContentsOfURL:document.documentURL options:NSDataReadingUncached error:nil];

    NSString *fullPath = [self documentFilePathWithDocument:document];

    if([[NSFileManager defaultManager] createFileAtPath:fullPath contents:data attributes:nil])
    {
        return [NSURL fileURLWithPath:fullPath];
    }
//OLD CODE
//   if([[NSKeyedArchiver archivedDataWithRootObject:data] writeToFile:fullPath atomically:YES])
//   {
//       return [NSURL fileURLWithPath:fullPath];
//   }

    return nil;
}

SO saving it with the filemanager and not with a keyedarchiver.所以用文件管理器而不是用keyedarchiver保存它。

If any of one have same problem even though you did everything suggestions above.如果任何人有同样的问题,即使你做了上面的所有建议。 (I had same problem, when I downloaded some files from google drive.) (当我从谷歌驱动器下载一些文件时,我遇到了同样的问题。)

Try this!尝试这个!

Put 'x' end of your file extension to be recognized as a new version of format.将文件扩展名的“x”结尾以被识别为新版本的格式。 (it's working only for 'doc' and 'ppt' files, not for 'xls' files) Yes, I know this is not a appropriate way to solve this problem, but it's worth to try it. (它只适用于“doc”和“ppt”文件,不适用于“xls”文件)是的,我知道这不是解决这个问题的合适方法,但值得一试。

Believe me I tried everything!相信我,我什么都试过了!

Hope you help.希望你能帮忙。

  1. Did you check if the size of the files is the same both online and offline?您是否检查文件的大小是否在线和离线都相同? It is possible that the file download wasn't complete文件下载可能未完成

  2. Try using the URL of the MS Office documents with a normal NSURL object and opening in a UIWebView.尝试使用 MS Office 文档的 URL 和正常的 NSURL object 并在 UIWebView 中打开。 Does it work then (so we know if its the document or your class)?那么它是否有效(所以我们知道它是文档还是你的类)?

  3. Try using NSURL's fileURLWithPath: method in the getDocumentFileAsPathWithDocument: It is possible that the URL being returned is incorrect (though doesn't look like it from the logs but doesn't hurt to try)尝试在 getDocumentFileAsPathWithDocument 中使用 NSURL 的 fileURLWithPath: 方法:返回的 URL 可能不正确(虽然从日志中看起来不像,但尝试并没有什么坏处)

first of all, use this code to make sure your documents are there,because i think the error cause by your documents path.首先,使用此代码确保您的文档在那里,因为我认为错误是由您的文档路径引起的。

NSFileManager *fileManager=[NSFileManager defaultManager];


        if([fileManager fileExistsAtPath:fullPath]){
            NSLog(@"%@  exsit! ",fullPath);
        }else{
            NSLog(@"%@  not exsit! ",fullPath);

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在线/离线显示3G和iPhone4的图片? - How do I show pictures online/offline for the 3Gs and iPhone4? iOS - QuickLook - 如何在没有UIScrollView的情况下在QuickLook中打开对象 - iOS - QuickLook - How to open an object in QuickLook without a UIScrollView 如何使应用程序显示在应用程序加载器中? - How do I get an app to show up in the Application Loader? 如何让倒计时器以天数显示倒计时? - How do I get the countdown timer to show the countdown in days? 当我尝试重用tableviewcell时,会出现重复的TableViewCells ...如何显示新的ViewViewcell? - Repeated TableViewCells show up when I try to reuse tableviewcells… how do I get the new ones to show? 如何获取核心数据以保存/获取文件? - How Do I Get Core Data To Save /Fetch My Files? iOS - QLPreviewController - 如何阻止 QuickLook 旋转? - iOS - QLPreviewController - How to stop QuickLook from Rotating? 如何获得iPhone尺寸的网页以显示正确的尺寸? - How do I get iPhone-sized web pages to show up the right size? 如何获得彩色纹理画笔出现在白色背景上的Open GL ES中? - How do I get a colored texture brush to show up in Open GL ES on white background? 如何使我的应用程序显示在iPhone的“打开位置...”列表中? - How do I get my app to show up in the “Open In…” List on iPhone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM