2011-06-02 65 views
3

是否有人知道如何使用UIDocumentInteractionController「在iBooks中打開」遠程pdf文件,我似乎無法繞過此文件。我已經設法打開我的PDF在QLPreviewController,並獲得OptionsMenu給我的選項打開iBooks,但我不會打開文件,如果它是遠程...當我使用本地文件,它工作正常。用於遠程pdf文件的UIDocumentInteractionController

如果這不可能有什麼替代方案?

在此先感謝

回答

3

雖然UIDocumentInteractionController有一個方便的方法interactionControllerForURL:,它需要的參數是一個文件URL。因此,您可以在您的應用程序中下載PDF並使用UIDocumentInteractionController對象打開它,也可以使用UIWebView對象打開遠程PDF。將網址傳遞給網絡視圖,並打開狀態良好。

+0

謝謝您的回答。關於如何在使用UIDocumentInteractionCntroller之前完成下載? – 2011-06-06 05:45:33

+0

我在我的應用程序中做同樣的事情,我正在使用[AFNetworking](https://github.com/AFNetworking/AFNetworking)。 請參見「創建下載任務」部分。您可以獲取文件的url路徑並在UIDocumentInteractionController中使用它。 – 2013-12-10 18:09:56

0

廣告提到你必須使用UIDOcumentInteractionController的文件url。首先下載文件。一個非常簡單的方法是使用AFNetworking。這裏是我如何使用AFNetworking下載文件:

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)req navigationType:(UIWebViewNavigationType)navigationType { 
self.title = req.URL.description; 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

NSURL *URL = req.URL; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 
    NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]]; 
    self.fileURLPath = [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]]; 
    return self.fileURLPath; 
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 
    NSLog(@"File downloaded to: %@", filePath); 
}]; 
[downloadTask resume]; 


return YES; 

}

現在,你有fileURLPath你可以像這樣創建UIDocumentInteractionController:

documentController = [UIDocumentInteractionController interactionControllerWithURL:self.fileURLPath]; 
+1

你確實意識到在這種情況下使用AFNetworking沒有任何好處,並且你對99%的代碼使用NSURLSession的東西.....而不是AFNetworking ... – TheCodingArt 2014-09-09 13:19:52