1

Well, i searched everywhere but i didn't have any luck with my situation. I am loading my webpage with UIWebView with the following code:

NSString *fullURL;
fullURL=@"http://domain.com";
NSURL *url=[NSURL URLWithString:fullURL];
NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];

I want to load the remote HTML file but load the images from the bundle resourses. The HTML file looks like this:

<img src="http://domain.com/images/image.png" width="20px" height="20px"/>

Can this be done? The majority of the posts over the internet(and here) are for loading local HTML with local images/resources which is different in my case.

Any help with my code? Thanks

Shah Paneri
  • 729
  • 7
  • 28
Theodoros80
  • 796
  • 2
  • 15
  • 43
  • 1
    You could try to parse innerHTML looking for bundle images links, then modify appropriate `img` tags with base64 content: http://stackoverflow.com/a/10713277/792677. I don't think that's a good solution, but at least that's something. – A-Live Apr 18 '13 at 09:11

3 Answers3

1

It would probably be better to use some more "loosely coupled" solution - not to edit the HTML code itself in some hacky way (regexp, not regexp... changing the HTML code manually is still pretty hacky).

As a matter of fact, I believe it can be done. The iOS just needs to be somehow informed that some of the assets are available offline, from the bundle.

Basically, when UIWebView is loading a page, first thing that happens behind the scene is downloading the main *.html page. Then all the graphics/css'es/js'es etc are being downloaded.

You can let the html file be downloaded "as is", but intercept those requests that are going to download graphics (+ other assets) and provide them from local bundle. Please refer to this post:

http://robnapier.net/blog/offline-uiwebview-nsurlprotocol-588

and apply appropriate changes. The big win here is that from the webview's (and code that loads / maintain via JavaScript calls it's content) perspective - nothing has changed at all. Only the custom NSURLProtocol itself knows that some data was loaded from local storage.

kajot
  • 303
  • 1
  • 9
1

Had the exact same problem. Subclassing NSURLCache to redirect the cache to cache images from local storage worked like a charm.

Here is the writeup that I followed: http://www.cocoawithlove.com/2010/09/substituting-local-data-for-remote.html

Andrey K.
  • 26
  • 2
0

TL;DR Complicated, avoid if you can, but possible.

If you still want to do it: don't do a UIWebView :loadRequest on the URL itself since it will trigger the start of downloading images very rapidly so modifying the images sources using Javascript will likely happen too late.

What you instead have would have to do is to download the contents of the URL on the native side and iterate the image tags there replacing the sources (quite complicated, don't use Regular Expressions to parse HTML btw, there are libraries for that), then injecting the modified HTML using UIWebView loadHTMLString:baseURL:.

Community
  • 1
  • 1
Jacob Oscarson
  • 6,363
  • 1
  • 36
  • 46
  • I see! If i understand correctly i should avoid it,according to you and A-Live. My main reason behind this was to optimize my app for faster loading of content. So i think i should approach this in different way. Thanks,i'll wait a little to see what other user suggest. – Theodoros80 Apr 18 '13 at 09:47
  • You will load and parse the files content on detached thread anyway (avoiding freeze of UI), as a user I'd prefer to see the document displayed without the images and let them load (or rather let me to recognize the document and leave it before they're loaded). – A-Live Apr 18 '13 at 09:57
  • @Theodoros80 yes, you understand it right. Also correct that the reason for doing/not doing this should be all about performance (the network of course always being many orders of magnitude slower than local access). – Jacob Oscarson Apr 18 '13 at 11:28