iOS 下载并保存 HTML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5606915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS download and save HTML file
提问by AveragePro
I am trying to download a webpage (html) then display the local html that has been download in a UIWebView.
我正在尝试下载网页 (html),然后在 UIWebView 中显示已下载的本地 html。
This is what I have tried -
这是我尝试过的-
NSString *stringURL = @"url to file";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"index.html"];
[urlData writeToFile:filePath atomically:YES];
}
//Load the request in the UIWebView.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]]; // Do any additional setup after loading the view from its nib.
But this gives a 'SIGABRT' error.
但这会产生“SIGABRT”错误。
Not too sure what I have done wrong?
不太确定我做错了什么?
Any help would be appreciated. Thanks!
任何帮助,将不胜感激。谢谢!
回答by Anne
The path passed to the UIWebView is incorrect, like Freerunnering mentioned, try this instead:
传递给 UIWebView 的路径不正确,就像提到的 Freerunnering 一样,试试这个:
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:@"http://www.google.nl"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
Note: Correct error-handling needs to be added.
注意:需要添加正确的错误处理。
回答by Keab42
NSDocumentDirectory will be backed up to iCloud. You might be better off using NSCachesDirectory https://developer.apple.com/library/ios/#qa/qa1719/_index.html
NSDocumentDirectory 将被备份到 iCloud。您最好使用 NSCachesDirectory https://developer.apple.com/library/ios/#qa/qa1719/_index.html
回答by Kyle Howells
Your app is a folder with a .app extension on the end. On the iPhone once it is installed you can't change this folder, hence why you save it to the documents directory.
您的应用程序是一个文件夹,末尾带有 .app 扩展名。在 iPhone 上一旦安装就无法更改此文件夹,因此您将其保存到文档目录中。
In your example code you save the file to Documents/index.html and ask it to load appname.app/index.html.
在您的示例代码中,您将文件保存到 Documents/index.html 并要求它加载 appname.app/index.html。
[NSBundle mainBundle] doesn't give you the documents directory it gives you (i think) the .app folder (though it might be the folder that contains the app, documents, and other folders).
[NSBundle mainBundle] 没有给你它给你的文档目录(我认为).app 文件夹(尽管它可能是包含应用程序、文档和其他文件夹的文件夹)。
To give you'll want to change this line.
为了给你想要改变这一行。
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
To (providing this is the same method as the rest of the code, else recreate the object 'filePath')
To(假设这是与其余代码相同的方法,否则重新创建对象“filePath”)
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath isDirectory:NO]]];
回答by code4latte
Here is the Swift 2.x version:
这是 Swift 2.x 版本:
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var filePath: String = "\(paths[0])/\("index.html")"
// Download and write to file
var url: NSURL = NSURL(string: "http://www.google.nl")!
var urlData: NSData = NSData.dataWithContentsOfURL(url)
urlData.writeToFile(filePath, atomically: true)
// Load file in UIWebView
web.loadRequest(NSURLRequest(URL: NSURL.fileURLWithPath(filePath)))