1

I'm currently attempting to download a webpage using a NSURLSession in order to retrieve a status update on the download progress. Unfortunately, after downloading the webpage, when I go to load the webpage, there are visual issues (missing images, missing javascript, missing styles, etc), leaving the webpage looking broken and in complete. When loading directly to the webView, everything loads correctly. I would like to know if there is a way (or anything I'm missing) to download ALL aspects of the webpage and load them up so I may load the full webpage and display a loading bar while the page loads.

   - (void)loadRequest:(NSURLRequest *)request
{
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    self.urlSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    self.downloadTask = [self.urlSession downloadTaskWithRequest:request];
    [self.downloadTask resume];
}

    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL
{
    [_webView loadData:[NSData dataWithContentsOfURL:downloadURL] MIMEType:downloadTask.response.MIMEType textEncodingName:downloadTask.response.textEncodingName baseURL:nil];
}
TheCodingArt
  • 3,436
  • 4
  • 30
  • 53
  • You didn't read my full post. I noted above that I want to be able to display a loading bar with the download progress. Hence why I am using this method. I need to know how to retrieve all of the extra files and such using this methodology. The loading property is about as useless as it gets IMO. It doesn't provide a download status and informs you that the loading is done even is JavaScript content is still loading. – TheCodingArt Mar 01 '14 at 15:51

2 Answers2

0

I was having similar problems (missing images etc.). You need to provide a baseURL. Try:

[_webView loadData:[NSData dataWithContentsOfURL:downloadURL] MIMEType:downloadTask.response.MIMEType textEncodingName:downloadTask.response.textEncodingName baseURL:downloadTask.response.URL];
nico
  • 156
  • 5
0

In order to display a loading bar you have to make some kind of workaround, because you are not able to know the progress of the resources that are being downloaded.

Many apps use tricks, to simulate that there is a loading ongoing. Here is a simple example to ilustrate the idea:

  • Display loading bar with progress 30% for 1 second.
  • If the request did not finish, display progress 90% until the progress finishes
  • When the request finishes, animate progress to 100%.

You can have a look on this open source library to see how it's done. You can do it in a similar way. https://github.com/ninjinkun/NJKWebViewProgress

raver99
  • 99
  • 1
  • 4