【问题标题】:iOS download and save HTML fileiOS 下载并保存 HTML 文件
【发布时间】:2011-08-02 04:34:34
【问题描述】:

我正在尝试下载网页 (html),然后在 UIWebView 中显示已下载的本地 html。

这是我尝试过的-

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.

但这会产生“SIGABRT”错误。

不太确定我做错了什么?

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 查看调试器输出。可能有消息。

标签: iphone xcode ios ios4 uiwebview


【解决方案1】:

这里是 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)))

【讨论】:

    【解决方案2】:

    NSDocumentDirectory 将被备份到 iCloud。你最好使用 NSCachesDirectory https://developer.apple.com/library/ios/#qa/qa1719/_index.html

    【讨论】:

    • 最好是评论。
    【解决方案3】:

    传递给 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]]];      
    

    注意:需要添加正确的错误处理。

    【讨论】:

    • 在您的情况下,无法下载图片等包含的资源
    【解决方案4】:

    您的应用是一个末尾带有 .app 扩展名的文件夹。在 iPhone 上安装后,您无法更改此文件夹,因此您将其保存到文档目录。

    在您的示例代码中,您将文件保存到 Documents/index.html 并要求它加载 appname.app/index.html。

    [NSBundle mainBundle] 没有给你它给你的文档目录(我认为).app 文件夹(尽管它可能是包含应用程序、文档和其他文件夹的文件夹)。

    为了给你会想要改变这一行。

    [web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
    

    To(假设这是与其余代码相同的方法,否则重新创建对象'filePath')

    [web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath isDirectory:NO]]];
    

    【讨论】:

      猜你喜欢
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多