iphone app UIWebView cache data - objective-c

I need a UIWebView to show a html page. if internet is not available it will show the last downloaded version. if has internet then it downloads the most current version and replaces and shows the version just downloaded. how can I do this? Thanks

You could implement you webViews delegate webViewDidFinishLoad: method to grab the request and load it again. Since it was just finished loading, we can assume that it is cached and load it with synchronous networking. Store the result. When network is unavailable load that result into the webView using loadData:MIMEType:textEncodingName:baseURL:. When network comes available agin just call loadRequest: to refresh.
Another option is to use stringByEvaluatingJavaScriptFromString: with a javascript around:
document.getElementsByTagName('html')[0].outerHTML;
and inserting the content with loadHTMLString:baseURL:
But these two only work for pure HTML without images, sounds, etc.
A third option would be to use the NSURLRequestReturnCacheDataDontLoad when network is not available. see NSURLRequest
You migth also have a look at the NSURLCache

Related

Force PreloadJS to use XHR for all loading

I'm using PreloadJS as part of an application that's being built with the CreateJS libraries. PreloadJS is being used to pull in graphics and audio files listed in a manifest. I've setup a progress bar and hooked the preloader's fileprogress and fileload events to update it. I'm getting progress updates as the images load, so I can see the progress bar crawling along, but I never get fileprogress updates for the audio files, just the fileload (file is completely loaded) so the progress bar sits idle and then suddenly jumps to 100%. Since the audio files are by far the largest assets, this creates something of a problem as far as providing meaningful load progress to the user.
I've looked at the documentation for PreloadJS and it indicates that XHR loading is the preferred method because it does provide the progress updates, but that PreloadJS can fall back on things like tag-based (<audio>) loading of audio files.
None of the file loading is local or cross domain, so it would seem to me that PreloadJS ought to be using XHR.
Is there a way to force PreloadJS to use XHR for everything so I can get consistent progress updates or why would I not be getting progress events for these much larger files?
I believe there are open bugs in PreloadJS and SoundJS which prevent audio loading from reporting progress. I have logged issues for both libraries:
https://github.com/CreateJS/PreloadJS/issues/99
https://github.com/CreateJS/SoundJS/issues/119
Some additional info:
Although PreloadJS will try and favor XHR-based loading for filetypes it controls, audio loading works a little differently.
SoundJS actually injects the functionality into PreloadJS to handle loading, and will not respect the useXHR parameter, instead relying on the browser capabilities (and SoundJS plugins) to load and play audio. Unfortunately, web audio requires an array buffer (loaded with XHR), whereas HTML audio requires HTML tags, so the playback capabilities dictate how audio files load.
By default, SoundJS will default to load/use the following plugins in order:
WebAudio (therefore XHR)
HTML (therefore tag-loading)
This should favor XHR-loading and webaudio for most browsers (IE is the standout that will almost always require HTML loading). You can force plugin order by registering the plugins manually before you begin playback/
Thanks for the surfacing this!
#Lanny is correct, there are open bugs for this issue. Currently SoundJS is setup in a way that always uses Tag loading with PreloadJS, even when using WebAudio which loads via xhr. The result is that regardless of plugin, there is currently no way to get progress events.
The good news is that we are currently in the process of revising how loading works between PreloadJS and SoundJS and this issue should be resolved.

UIWebView opens safari on load

I have a UIWebView in my application. If I tell it to open the url #"http://twitter.com/" it open in that same webView fine. But if I ask it to open a url like (similar to the one I need it to open): #"feed://www.OcataCore.com/rss/news.xml" it opens up safari as soon as the view that contains the UIWebview is loaded. Is there a way i can force that url to be opened in my webview ?
UIWebView does not offer the same range of features as Safari does. Except html, UIWebView is capable to read the formats listed in this Q&A. It appears like it is not capable of displaying XML, no matter if it is taken from a feed or a local file.
agreed, should not be able to open XML files.
hope this helps though if you are just wondering how to open Safari. use this delegate method of UIWebView to open the links.
webView:shouldStartLoadWithRequest:navigationType
that should work.

UIWebview javascript leak?

I've posted about this before but have been struggling to come up with a solution.
Basically I have a HTML5/jQuery app within my iPad app. Every time I load an image into the UIWebView (HTML App) the overall allocations in the profiler increases by about 2MB each time. This sounds about right because the image is approx 2MB's. I am using the data notation in the tag to load a Base64 image.
i.e.
When I load a certain number of images (page turns) the app will crash.
The app is an ebook viewer, so when I turn to a new (not previously loaded in current session) the allocations increase. But, if I turn back to a previiously loaded page the allocations don't increase and the page loads quicker than a new one. Every page turn sends a request to the database so i'm beginning to think the leak isn't in the iOS and that it could be in the HTML5 app.
Any ideas on this? I guess there could just as easily be a leak in the HTML app as there could be in iOS. How do I go about debugging this?
Any ideas greatly appreciated.
Thanks
HTTP and WebKit likes to keep a local copy of resources, just in case you will need it again. This may be what you encounter.
Check the answers to this question: Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?
This was die an unfixable issue with iOS 4.
Issue resolved itself after upgrading to iOS5.

How can I download files from UIWebView using ASIHTTPRequest?

I want to create a section of my app where you can download the current webpage showing in the UIWebView and then save it to a UITableView and when you tap on the filename in the UITableView, it will load the local file in the UIWebView to allow offline browsing.
How can I do this with http://allseeing-i.com/ASIHTTPRequest? I've taken a look and all the code looks very daunting for a beginner! Is there an alternative method? Is this the best method?
Thank you
ASIWebPageRequest is the answer. It's a class included with ASIHTTPRequest that makes it easy to download whole pages, with all their associated dependencies.
You'd probably want to create a custom cache to store your downloaded webpages in, and then load them out of that cache when requested by the user.

Loading images from to differenet base resources to the webview

I am implementing a WebView in which I need to use some images. For this, I use the images under the ressources folder and call
[myWebView loadHTMLString:returnString baseURL:[[NSBundle mainBundle] resourceURL]];
It is working fine. In the same time I need to use some external images coming from the server in the same WebView. For this I need to change the baseURL of the target server but I don't know how to handle these two.
How to handle both images, from local and external server in one HTML?
If you use full URLs (i.e. http://www.exmaple.com/myimage.png) for the images coming from the server, they won't be affected by the baseURL parameter, which is only used for resolving relative (partial) URLs.
I think you can create two instance of UIView class and add the UIWebView in both the views.
In the first webview show the image from the resource and in the other webview show the image from the server.
Just give it a try see if this works.