如何从xcode中的文档目录加载uiwebview中的pdf文件?

时间:2013-10-25 05:46:06

标签: xcode uiwebview filepath nsdocumentdirectory

我在文档目录中下载我的pdf文件,然后尝试显示该pdf文件

从那条路径加载到uiwebview但是它没有打开一个空白的白页,我不

理解为什么它没有加载到uiwebview。

下面是我的代码,

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 

NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath = [documentsDirectory stringByAppendingPathExtension:@".pdf"];

NSURL *url = [NSURL fileURLWithPath:filePath];


NSURLRequest *request = [NSURLRequest requestWithURL:url];

[self.webView loadRequest:request];

我的文件路径是:

NSLog(@“Filepath ------->>%@”,filePath);

/ Users / zee / Library / Application Support / iPhone Simulator / 6.0 / Applications / BD79829B-95FF-4B91-94AD-830C6B1CD31D / Documents / myPDF.pdf

请告诉我为什么它不起作用以及什么应该是最好的解决方案

由于

Zeeshan Shaikh

1 个答案:

答案 0 :(得分:3)

试试这个,

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 

NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath = [documentsDirectory stringByAppendingPathExtension:@"sample.pdf"];

NSURL *url = [NSURL fileURLWithPath:filePath];

NSString *urlString = [url absoluteString];

NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *webURL = [NSURL URLWithString:encodedString];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

[self.webView loadRequest:request];