iOS download and save HTML file

ぐ巨炮叔叔 提交于 2019-12-17 15:44:10

问题


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

Not too sure what I have done wrong?

Any help would be appreciated. Thanks!


回答1:


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.




回答2:


NSDocumentDirectory will be backed up to iCloud. You might be better off using NSCachesDirectory https://developer.apple.com/library/ios/#qa/qa1719/_index.html




回答3:


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.

In your example code you save the file to Documents/index.html and ask it to load 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).

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')

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



回答4:


Here is the Swift 2.x version:

    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)))


来源:https://stackoverflow.com/questions/5606915/ios-download-and-save-html-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!