Fetching/Cashing Data From UIWebView - iphone-sdk-3.0

I was wondering if there is anyway to get what is being displayed in UIWebView. e.g; you have opened bbc.co.uk in UIWebView and you wanted to save all the content of that page, Is there anyway to do that?

I think that Webkit on iPhone uses the standard [NSURLCache sharedURLCache] for caching. If that is the case, you can implement your own cache or query the cache for existing items. Try:
[[NSURLCache sharedURLCache] cachedResponseForRequest:myRequest];
Implementing your own cache is a more reliable solution, because the system cache may choose not to cache anything.

Related

IOS7: Webview memory leak

I know this is a very recurring topic but, i tried all the suggested solutions they seem not to work...
The scenario:
I load an empty view and i add at run time an UIWebview.It is a global variable,initiated only once.
the initial memory usage (by Xcode 5.1) is ~6MBytes.
i will load in the view a series of url using a button event.Each time is pressed a new page will be loaded.
i load in the webView the first page from internet with few image and one video.
After the loadRequest is completed, the memory is ~20Mbytes.
Every time i load a new page the memory increases.
The memory stays the same even when i load a new controller...
What I tried so far:
To use the [NSURLCache sharedURLCache] and related functions to instantiate a fixed amount of cache in the Application delegate: the App does not care...memory changes despite the limit of 32MBytes i set.
To set to nil the global variable: no effect.
To use temporary UIWebview(s) instead of the global one; views are removed from the superview each time the controller changes: no effect.
None of this tentatives have been successful...the memory increase and it is never released.
If i start the video from the web browser and then i load a new controller, the sound of the video still runs in the background...
What am I missing???
Thank you in advance

After exiting app, how to go back to the exact same screen where I left off?

Is there a quick and easy way to have the app go back to the same screen after I put it in background mode?
I know there are frameworks just for this stuff, but has anyone done it without much sweat?
Thanks
In your app delegate, add a line to the applicationDidenterBackground: method that stores the current page via NSUserDefaults.
Then in the applicationWillEnterForeground: method, load up that saved value and restore the page.
Multitasking should enable this, while a device has enough memory to keep your app open. Otherwise how about using NSUserDefaults to store a string reference to your view?
As of iOS 5.1, there is no quick and easy way.

UIView and UIImage preloading

I've got magazine app and I want to know If is there any way how to preload UIView and UIImages?
My views structure:
MagazineView
-> pageView
-> imageContainerView
-> image
-> imageContainerView
-> image
-> textView
-> pageView
etc...
So my question is - How to preload images before views are visible? I want to make some Cache with 3 or 5 pages and makes threads for loading views. Any ideas?
I presume that to 'preload images' means downloading them before they are actually used so there is no delay when displaying them.
What you could do is download the images first using [NSData dataWithContentsOfURL:], then either dump the data into a file using writeToFile:atomically: on your NSData instance, or simply keep the data in heap in the NSData for future reuse.
Then when you need it, you can create UIImage using either [UIImage imageWithContentsOfFile] or [UIImage imageWithData:].
Note that if you save the cache to files, save those files in the cache folder, not in the documents folder, or mark them with the attribute that makes them skipped when syncing to iCloud, otherwise your app will be rejected by Apple.
Also keep in mind that downloading those images can taken long, timeout, or fail. You should do all this in a background thread in a way that does not block the main thread, have a visual indicator that the application is not frozen and have a fallback for the case where the downloads fail or the cache is flushed (if you save the images to files)
After you load images as J_D described, just put them into pageView and set them Hidden to YES. Every time you want to switch page, just call some method, that will pre-load new images and show those, which are already downloaded (set Hidden to NO) ;-).
Hope this will help you to solve your problem...

iOS - Loading an image from a web server in Objective C

At the moment, I have a goal of writing an application that contains a UIImageView. Upon loading or pressing a button, an image from a web server will load into the view.
What is the simplest way to do this? I'm assuming using the API described here? https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
In the mean time, I will continue to try to get this working on my own. Any advice would be appreciated, especially if you have written code like this before.
Yes, this can be done by making a request through an NSURLConnection.
You might be interested in checking out SDWebImage. This library includes a category that adds a method to UIImageView that makes asynchronously loading an image from a remote URL as simple as this:
[self.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];

Creating UIWebview programmatically and using it to do a couple of things then destroy it?

Is it possible to create a hidden UIWebView programmatically and then release it(destroy it completely deallocating all of it's memory) when I have used it? If so, could you give some tips. Thanks!
It seems odd that you want to use a hidden UIWebView object for some task when the main purpose of an UIWebView object is to present the html content. The answer to your question is yes. You can create them and remove them programmatically just like any other view as #Nil has mentioned but it seems your main purpose behind this is to get the content. If that's so, you have many other ways you can get that content without having to create a UIWebView object. You can use NSString's initWithContentsOfURL:encoding:error: method to get the contents of the url into a string which you can parse and use. NSData has a similar method. However to use these you will have to perform them in the background otherwise they will block the main thread. The most obvious and better way would be to use NSURLConnection or its popular counterpart ASIHTTPRequest.
Yes, you can create and destroy a uiwebview as any other uiview.