iOS download and save HTML file

前端 未结 4 808
悲哀的现实
悲哀的现实 2020-12-05 05:12

I am trying to download a webpage (html) then display the local html that has been download in a UIWebView.

This is what I have tried -

NSString *st         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 06:06

    The path passed to the UIWebView is incorrect, like Freerunnering mentioned, try this instead:

    // 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.

提交回复
热议问题