我如何在UIWebview中从NSHomeDirectory()加载文件?这是可能的 ?我的意思是从/ Documents /文件夹加载.pdf文件。从NSHomeDirectory()在UIWebView中加载文件?
谢谢!
我如何在UIWebview中从NSHomeDirectory()加载文件?这是可能的 ?我的意思是从/ Documents /文件夹加载.pdf文件。从NSHomeDirectory()在UIWebView中加载文件?
谢谢!
我不知道该怎么DOWS NSHomeDirectory()
作品,但你可以看看this post来看看如何获得这些文件的完整路径和this other看看如何将一个PDF
加载到UIWebView
这里是你如何可以下载,阅读和本地存储PDF的iPhone应用程序:
首先创建一个UIWebView并包括< < UIWebViewDelegate >>
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YourPDF.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
// 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];
梦幻般的答案,应该标记为正确的! – Patrick
加载PDF转换成Web视图不是最优的。看看[**'QLPreviewController' **](http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/QLPreviewController_Class/Reference/Reference.html)。 –