2012-02-20 24 views
1

目前,我通過檢查當前的URL來檢測UIWebView是否加載PDF文件。接下來我用ASIHTTPRequest庫下載pdf文件。問題是,如果文件顯示在UIWebView中,它是否已經下載到某處,所以我下載了這個文件兩次。我怎樣才能得到這個文件加載在我的UIWebView?如何將pdf文件加載到我的Documents目錄中的UIWebView中?

目的是將此文件加載到我的UIWebView中加載到我的Document目錄中。

+0

Duplicate:http://stackoverflow.com/questions/2832245/iphone-can-we-open-pdf-file-using-uiwebview – Uko 2012-02-20 06:59:31

+0

是不是同一個問題,我想存儲一個pdf已經加載在我的文檔目錄中,不在UIWebview中加載它。 – Anthony 2012-02-20 07:06:12

+0

你想自動執行還是當用戶提問?因爲不是從網上顯示PDF,您可以下載並存儲它,並在Web視圖中顯示它。根據http://www.iphonedevsdk.com/forum/iphone-sdk-development/5608-uiwebview-how-get-content-shown.html#post333645 pdf數據無法從Web視圖獲得 – Uko 2012-02-20 07:21:51

回答

1

這裏是你如何下載,閱讀和本地存儲PDF的iPhone應用程序,讓你不必定期下載:

首先創建一個UIWebView幷包括< < UIWebViewDelegate >>

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YourPDF.pdf"]; 
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){ // if file not present 
      // download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link 
     NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://s3.amazonaws.com/hgjgj.pdf"]]; 
      //Store the downloaded file in documents directory as a NSData format 
     [pdfData writeToFile:filePath atomically:YES]; 
    } 

    NSURL *url = [NSURL fileURLWithPath:filePath]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [yourWebView setUserInteractionEnabled:YES]; 
    [yourWebView setDelegate:self]; 
    yourWebView.scalesPageToFit = YES; 
    [yourWebView loadRequest:requestObj]; 

或者,如果你只是想從你的資源文件夾讀/加載PDF然後簡單地做到這一點:

 NSString* filePath= [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"]]; 
    /// or you can even read docs file as : pathForResource:@"sample" ofType:@"docx"] 
    NSURL *url = [NSURL fileURLWithPath:filePath]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [yourWebView setUserInteractionEnabled:YES]; 
    [yourWebView setDelegate:self]; 
    yourWebView.scalesPageToFit = YES; 
    [yourWebView loadRequest:requestObj]; 
+0

謝謝,它的工作原理。 – Bhimbim 2017-08-15 08:42:33

0

我只能再次下載它,或者把它放在臨時存儲器中,然後把它放在UIWebView中,當用戶詢問時,然後將它放到臨時存儲器所需的位置。

相關問題