1

I am saving the content of UIWebView so as to display it on the next launch rather than again loading it. This is what I am doing:

NSString *activityesContent = [webView stringByEvaluatingJavaScriptFromString:@"document.body.outerHTML"];
        [[NSUserDefaults standardUserDefaults]setObject:activityesContent forKey:@"activityesContent"];  

And for the next launch I am doing this:

[_tgWebView loadHTMLString:[[NSUserDefaults standardUserDefaults]objectForKey:@"activityesContent"] baseURL:nil];  

This loads the page but without any JavaScript or CSS. It is just a basic HTML content. I know I can load the JavaScript using stringByEvaluatingJavaScriptFromString. But is there somehow I can get all the JavaScript and CSS on the page as I am getting the HTML content, so that I can use it later on?

I also tried with document.body.parentNode.innerHTML and document.documentElement.outerHTML, but still basic HTML is displayed. The reason for this is, the images, CSS and JavaScript has a server path. I have asked one more question but it doesn't work for my HTTPS requests.

Neither does this work:

NSError *error = nil;
        NSString *activitiesContent = [NSString stringWithContentsOfURL:[NSURL URLWithString:TGURL_ACTIVITIES] encoding:NSUTF8StringEncoding error:&error];

I tried this solution which is being referred at most of the places. But this gives me a blank page. I even tried to save the contents in a file. When I accessed the source of the page in the file, I found that the path of CSS and JavaScript is relative:

<link href="/Content/Sass/style_sass.css?version=1.0.1.09" rel="stylesheet" />  <!-- call style_sass.css file -->
    <link href="/Content/Sass/topbar.css?version=1.0.1.09" rel="stylesheet" />  

If I try creating resource folder, the size of this folder will be huge. Plus there will be a maintenance issue. I have to update the CSS/JavaScript each time there is a change in server CSS/JavaScript.

This is what I am getting:

enter image description here

And this is what the page should be:

enter image description here

Finally what I want:

What is the best fix for this or is there some other way I can show the entire page (including CSS, JavaScript, images) on the app's further launches ?

Community
  • 1
  • 1
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • You do it in wrong way, because you newer save any external resources like images, css and js, and there is no any easy way to save them. I can suggest you to use subclass of `NSURLCache` and store entire request from server – sage444 Mar 11 '15 at 09:56
  • @sage444 : Please provide some idea on implementation of what you are suggesting. – Nitish Mar 11 '15 at 10:00
  • google around it and check apple's sample for begining https://developer.apple.com/library/ios/samplecode/URLCache/Introduction/Intro.html – sage444 Mar 11 '15 at 10:24
  • @sage444 : Not useful. – Nitish Mar 12 '15 at 07:20
  • http://robnapier.net/blog/offline-uiwebview-nsurlprotocol-588 I think this explains whole story – yogs Mar 18 '15 at 06:38
  • Hi Nitish - a bit of feedback following my edit. Ordinary technologies such as SQLite, CSS, JavaScript etc. do not need to be rendered either in bold or inline code formatting: they are proper nouns and so either an initial capital or all-caps (for acryonyms) is more than sufficient. Inline code formatting is useful for class names, variables, and console I/O. Hyperlinks also do not also need emboldening - just use the standard stylesheet, to offer some readability and presentational consistency with other questions. Thanks! – halfer Nov 01 '15 at 20:55

2 Answers2

1

Instead of using the UIWebView to load the content initially you can make a direct request and download the full data. When the data is downloaded then you can load the UIWebView with the downloaded content. When using document.body.outerHTML you are effectively only getting the body tag.

Arash
  • 1,286
  • 1
  • 8
  • 14
1

Your best bet would be to use NSURLProtocol to observe the HTTP requests for the pages and write them to disk as well. You can then later serve the files from disk rather than reloading them over a HTTP connection also by using NSURLProtocol.

Ray Wenderlich has a great tutorial on how to use NSURLProtocol for basically this purpose.

Ell Neal
  • 6,014
  • 2
  • 29
  • 54