我如何在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)。 –