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

标签 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(@"文件路径------>> %@", 文件路径);

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

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

谢谢

齐山谢赫

最佳答案

试试这个,

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];

关于xcode - 如何从 xcode 中的文档目录加载 uiwebview 中的 pdf 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19581971/

相关文章:

java - 如何在没有硬编码文件路径的情况下从 Java 运行 Python 脚本?

java - 如何转义java中文件路径中的反斜杠和自动生成的转义字符

ios - 如何允许我的 Xcode 源代码编辑器扩展使用 XPC?

SwiftUI - 类型 '[Color]' 的值没有成员 'identified'

css - 溢出 : scroll not working in UIWebView in iOS 8

iphone - 如何在 iPhone 的 UIWebView 中正确保存/显示换行符?

python - 如何使用 sys.path.append 在 python 中导入文件?

ios - 我怎么知道我是否在使用私有(private)框架?

iphone - 要下载哪个 iPhone SDK?

jquery - UIWebView 不会导致 document.ready 触发