How to set cache expiration time when using UIWebView and NSURLRequest? - iphone

I'm looking for the most simple default solution. Currently, I have one about view controller where some about info from some url is shown. I need to cache that for offline usage, and cache should be updated after some time, for example after a week. Currently , I'm using NSURLRequestReturnCacheDataElseLoad cache policy but don't know how to set cache expiration and cache update time:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSString *urlString = [NSString stringWithFormat:#"%#%#", kServiceBaseUrl, #"docs/about_en.html"];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[self.webView loadRequest:request];
}
I have read several posts where manual cache handling (get NSData, save, check and load) is suggested. But maybe there's more straightforward and simple solution?

You can manually clear the cache like this:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
As for expiration and handling options, take a look at the NSURLRequest Class Reference dealing with cache here.
You can get a detailed explanation of Understanding Cache Access from the Apple URL Loading System Programming Guide here.

Related

how to show rss feed url in UIWebView in iphone sdk?

I am trying to display url in UIWebView but I can not display, webview just call the fail with error method. My url is this "http://www.tekniknoktamarket.com/index.php?route=feed/google_base",
So please help me how to display this url in UIWebView *?*
In order to have a UIWebView download and display the content from a URL, you need to go through a couple of layers of abstraction, the first being an instance of NSURL, which contains your URL string. Then, you hand the NSURL to an instance of NSURLRequest. Finally, you hand the NSURLRequest to the webView. It all looks something like this:
NSURL *url = [NSURL URLWithString:#"www.your-url.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[yourWebView loadRequest:request];
That should do it.
This is the code loading a URL into a webview:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.theurl.com"]]];
If you're using this code and are encountering any problems, please provide the code you were using and the exact error

how to pass value through NSURL

i was trying to pass value to a native html page from my web view application
I try
url=[NSString stringWithFormat:#"%#?bob=123&frank=321&tom=213",url];
but when i build and debugging program, application crashes showing “EXC_BAD_ACCESS”
Please help me if anyone knows how to do this.
The UIWebView class only supports loading of URLs using the NSURLRequest class. To do what you want to do, you'll need this:
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:#"%#?bob=123&frank=321&tom=213", base]];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];

Why UIWebView Eating so many Memory?

I'm using a UIWebView to load a pure Text HTML page for my iPad app. The size of the HTMP page is only 40KB. But when I use the instrument to monitor the memory use for loading the UIWebView, I found down it consumes like 20MB memory, if I scroll the web view, the memory is even getting higher. Finally I get a level 1 memory warning.
Could anyone help me with this? How could I reduce the memory for this? (I need to use the HTML to show the text here).
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"SPC"];
NSURL *url = [NSURL fileURLWithPath:htmlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
htmlPath
url
request
release all of them after this line
[webView loadRequest:request];
then release webview in dealloc and use webview as ivar

Loading NSMutableDictionary from URL

I'm currently doing this:
NSMutableDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL: [NSURL URLWithString:#"http://mysite/mypage.php"]];
Which is great, apart from when the data being returned is quite large, and it appears to time out. How could I get around this?
Thanks in advance.
I'm not a big fan of using NSDictionary to manage downloads. I'd probably try something like:
NSURL *url = [NSURL URLWithString:#"http://mysite/mypage.php"];
NSURLRequest *request = [NSURLRequest requestWintURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
Now, if data is not NULL then save to local file. Then load the dictionary using the contents of that file using the initWithContentsOfFile: method.
If you still get the timeouts you can try larger timeoutIntervals.
NSURLRequest (or NSMutableURLRequest) and NSURLConnection.
In convenience methods like initWithContentsOfURL you have no control over things like timeouts. They are fine in my cases but it sounds like you will need to use the more low-level NSURLConnection and NSURLRequest to load data from the server. There are many examples on the net.

caching the webcontent ( images ) on the iPhone

I have a question concerning the caching of webcontent.
I've created a UIWebview component
Code:
NSString *urlAddress = #"http://192.168.55.101/~test/mobile/iphone/ads/v0.1/";
//URL OBJECT footer
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320 , 100)];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
This shows a picture in my iPhone.
I've tried looking into cache attributes and functions (for instance NSURLRequestReturnCacheDataElseLoad ) to see which options I have , but what's the best way to cache the image appearing in this screen.
Or how do I use NSURLRequestReturnCacheDataElseLoad?
There is a good sample code:
Apple sample code
Description:
CacheInfo allows you to choose a URL, create a connection, load the resource asynchronously, and observe results such as the data size, load time, and cache usage. You can adjust the sizes of the shared memory and disk caches and observe how caching is affected during the load.