Pre-populate HTML5 offline appcache for a UIWebView in an iOS application
Asked Answered
O

3

13

Is it possible to create a UIWebView that has an HTML5 offline appcache pre-populated so that it will work offline even if it is the first time the user is accessing the UIWebView?

If so, how?

I know I can achieve this through other mechanisms, but the above is my first choice. And I'm just plain curious if it's possible.

I'm seeing nothing about it in the documentation.

Outsole answered 14/11, 2011 at 18:42 Comment(6)
Correct me if i'm wrong, but i believe you can open a local file in the UIWebView from the documents directory of your app.Discourteous
You're not wrong, but as I wrote in the question, "I know I can achieve this through other mechanisms, but the above is my first choice."Outsole
this is a pretty cool question. consider looking at only what is provided in the developer reference as apple will reject your app otherwise. you can consider 'force-loading' whatever you want in a hidden view to warm up the cache. in this way, you have the ability to add elements to the cache but you don't have the power to remove items from the cache unless you know the internal caching algorithms... I'd say this is less of a hack and more of a technique!Selfcontrol
@vinnybad: I'm not entirely sure what you mean by "force-loading". Can you elaborate on that? (Sounds like it might be worth putting in an answer rather than a comment!)Outsole
Sure, I'm adding a response now...Selfcontrol
Yes, that clears it all up. Thanks!Outsole
C
4

I think this may reduce to a question of whether you can programmatically write to the UIWebView's cache file (which is to say, whether the UIWebView cache resides within your app's sandbox) – if you can't, then game over. If you can, then it becomes a question of what happens after you drop a pre-populated cache file into place, and whether the UIWebView is "fooled" into thinking that it's already downloaded and cached your HTML5 content.

If you're using the iPhone Simulator to test your app, look in ~/Library/Application Support/iPhone Simulator/5.0/Applications (replace "5.0" with your iOS version, if necessary). You should see a long string of hex digits for each app you've compiled in the simulator; find the one that corresponds to your app, and then look in the Library/Caches/[your app's identifier] subfolder for a file named Cache.db.

This may be the place where UIWebView stores its cache data. If it isn't, game over and the answer to your question is "no, that's not possible". If it is where UIWebView caches data, then it may be possible to populate this Cache.db file in the simulator, grab the file, store it in your app bundle, and then write the cache to the appropriate location when it's time to pre-populate the cache.

At any rate, that's the line of attack I'd use to determine whether it's possible – I'm pretty confident the answer is going to be "no, not possible" unless it turns out the UIWebView cache does reside in your app's sandbox, is writable by you, and you can fool UIWebView by replacing its cache file.

Choosy answered 21/11, 2011 at 4:41 Comment(3)
I had to use information from both your answer and Deniz Mert Edincik's answer to make this happen. I wish I could just split the bounty 50/50. I may end up flipping a coin. Thanks!Outsole
Well, I got most of the way there, able to copy an appcache file to the right place, but then the app doesn't use it. This is all probably a bad idea anyway. As @Selfcontrol points out, Apple is likely to reject an app that does these sorts of shenanigans. But this has been very interesting (to me, anyway) and informative. Thanks for the information!Outsole
@Outsole According to section 3.3.2 of the Apple Developer Agreement, we are able to download interpreted code via the built-in Webkit framework to update hybrid iOS apps so long as the app remains consistent with its original advertised purpose. I'd like to start doing this with my own app, but I'm not sure how to handle pre-populating the UIWebView cache such that the app can be downloaded from the iOS App Store and then immediately used offline. How did you end up solving this?Windbroken
M
6

What you are looking for is two files in your cache folder.

ApplicationCache.db and cache.db

They both reside in Library/Caches/[your bundle identifier] folder for your application, which you have full access. You can add pre populated cache data to you bundle, and simply copy it to caches folder on you app launch.

BTW you can play with them easily, as they are simple, SQLITE db's.

I hope this helps

Marja answered 23/11, 2011 at 1:14 Comment(2)
I had to use information from both your answer and Scott Forbes's answer to make this happen. I wish I could just split the bounty 50/50. I may end up flipping a coin. Thanks!Outsole
Bummer, it let's me copy the file but then doesn't use it. Ah well. This has certainly been an interesting experiment.Outsole
C
4

I think this may reduce to a question of whether you can programmatically write to the UIWebView's cache file (which is to say, whether the UIWebView cache resides within your app's sandbox) – if you can't, then game over. If you can, then it becomes a question of what happens after you drop a pre-populated cache file into place, and whether the UIWebView is "fooled" into thinking that it's already downloaded and cached your HTML5 content.

If you're using the iPhone Simulator to test your app, look in ~/Library/Application Support/iPhone Simulator/5.0/Applications (replace "5.0" with your iOS version, if necessary). You should see a long string of hex digits for each app you've compiled in the simulator; find the one that corresponds to your app, and then look in the Library/Caches/[your app's identifier] subfolder for a file named Cache.db.

This may be the place where UIWebView stores its cache data. If it isn't, game over and the answer to your question is "no, that's not possible". If it is where UIWebView caches data, then it may be possible to populate this Cache.db file in the simulator, grab the file, store it in your app bundle, and then write the cache to the appropriate location when it's time to pre-populate the cache.

At any rate, that's the line of attack I'd use to determine whether it's possible – I'm pretty confident the answer is going to be "no, not possible" unless it turns out the UIWebView cache does reside in your app's sandbox, is writable by you, and you can fool UIWebView by replacing its cache file.

Choosy answered 21/11, 2011 at 4:41 Comment(3)
I had to use information from both your answer and Deniz Mert Edincik's answer to make this happen. I wish I could just split the bounty 50/50. I may end up flipping a coin. Thanks!Outsole
Well, I got most of the way there, able to copy an appcache file to the right place, but then the app doesn't use it. This is all probably a bad idea anyway. As @Selfcontrol points out, Apple is likely to reject an app that does these sorts of shenanigans. But this has been very interesting (to me, anyway) and informative. Thanks for the information!Outsole
@Outsole According to section 3.3.2 of the Apple Developer Agreement, we are able to download interpreted code via the built-in Webkit framework to update hybrid iOS apps so long as the app remains consistent with its original advertised purpose. I'd like to start doing this with my own app, but I'm not sure how to handle pre-populating the UIWebView cache such that the app can be downloaded from the iOS App Store and then immediately used offline. How did you end up solving this?Windbroken
S
1

What I mean by force loading is this: if a web page is visited, elements are cached for a faster load the next time. What I mean by force-loading or warming up the cache is to actually issue requests to the uiwebview in the background to load the data into the cache for faster loading so when the user actually clicks on it, it has the information in the cache.

If you use chrome, it's similar to their strategy they use to make surfing a little faster: when a page loads, they immediately find all links on the page and resolve the links so if a user clicks on the link, they don't have to wait for a response from the DNS servers before seeing the page as it's already done for them!

I hope this made a little more sense.

QUOTE:

this is a pretty cool question. consider looking at only what is provided in the developer reference as apple will reject your app otherwise. you can consider 'force-loading' whatever you want in a hidden view to warm up the cache. in this way, you have the ability to add elements to the cache but you don't have the power to remove items from the cache unless you know the internal caching algorithms... I'd say this is less of a hack and more of a technique! – vinnybad Nov 23 at 17:26

@vinnybad: I'm not entirely sure what you mean by "force-loading". Can you elaborate on that? (Sounds like it might be worth putting in an answer rather than a comment!) – Trott Nov 23 at 17:40

Selfcontrol answered 30/11, 2011 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.