2015-10-05 68 views
1

我想在Web視圖中顯示PDF文件。對於我寫的代碼,如:Webview未從文檔目錄加載PDF文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"201507234Surat_GDCR.pdf"]; 

self.pdfWeb.hidden=FALSE; 
self.JantriImage.hidden=TRUE; 
self.pdfWeb.delegate=self; 
self.pdfWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,ScreenWidth,ScreenHeight-64)]; 
self.pdfWeb.scalesPageToFit = YES; 
NSURL *url = [NSURL fileURLWithPath:filePath]; 
NSString *urlString = [url absoluteString]; 
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSURL *webURL = [NSURL URLWithString:encodedString]; 
NSURLRequest *request = [NSURLRequest requestWithURL:webURL]; 
[self.pdfWeb loadRequest:request]; 

這裏self.pdfWeb是我的WebView和文件已經在文件的目錄,但網頁流量無法加載文件。

回答

0

試試這個代碼:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"201507234Surat_GDCR.pdf」]; 

NSURL *url = [NSURL fileURLWithPath:filePath]; 

NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
+0

它沒有顯示出.. –

0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"201507234Surat_GDCR.pdf」]; 

success = [fileManager fileExistsAtPath:filePath]; 

if (success) 
{ 
    NSURL *url = [NSURL fileURLWithPath:filePath]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    [webView loadRequest:request]; 

    webView.dataDetectorTypes=UIDataDetectorTypeAll; 

}