3

我正在将我的 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);

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

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

谢谢

泽山谢赫

4

1 回答 1

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];
于 2013-10-25T06:15:02.337 回答