Hey guys in my APP i have created custom class for cache by inheriting NSURLCache class and implemented required methods to pass my local files data as cached data.I have done it same as this tutorial
In my APP there is one refresh button,on click of it i have to refresh the current page for this i have done following to load locally stored CSS and JS files
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if (isGoingToRefresh) {
[NSURLCache setSharedURLCache:cache];
isGoingToRefresh = NO;
return YES;
}
NSString *path = [mainWebView stringByEvaluatingJavaScriptFromString:#"window.location.pathname"];
path = [NSString stringWithFormat:#"%#%#",[[[AppDelegate sharedAppDelegate]configurationFile]objectForKey:#"RootURL"],path];
[NSURLCache setSharedURLCache:cache];
if ([path isEqualToString:[[request URL]absoluteString]]) {
[self showRefreshLoader];
isGoingToRefresh = YES;
NSURLRequest *localRequest = [NSURLRequest requestWithURL:[request URL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:240] ;
[webView loadRequest:localRequest];
return NO;
}
return YES;
}
Above code is loading data from cache by calling following NSURLCache method -
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
But on iOS4 webView not calling above NSURLCache method.
I am not getting why please suggest me some good solution for it.
try using to make your request
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLCacheStorageAllowed
timeoutInterval:60];
Related
I'm trying to load a UIWebView, but I can't understand why doesn't work, I'm doing this:
Loading *load = [[Loading alloc] init];
[load searchName];
then Loading.h
#interface Loading : NSObject <UIWebViewDelegate>
{
UIWebView *myWebView;
}
- (void) searchName;
Loading.m
- (void) searchName{
myWebView = [[UIWebView alloc] init];
myWebView.delegate = self;
NSString *urlAddress = #"www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[myWebView loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
NSString *yourHTMLSourceCodeString = [myWebView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
NSLog(#"%#",yourHTMLSourceCodeString);
}
why never call webViewDidFinishLoad method?
The reason your webview is not displayed is you have taken NSObject class and NSObject classes dont have an XIB and the way you have done is just calling your -(void)searchName method and is not adding it to self.view.
Did you created this webview with IB? Did you connected the delegate to the webview?
Try to catch errors with webView:didFailLoadWithError: (post them if there are errors)
Try to set the delegate with code [myWebView setDelegate:self]; or self.myWebView.delegate = self;
Check output of delegate if nothing happend:
self.myWebView.delegate = self;
NSLog(#"%#",self);
Yes, take note into what Nathan said. I am just expanding.
If you are using IB, connect the outlet. Then use [myWebView setDelegate:self];
Be sure to use http://. Http on it's own won't do anything.
Can you get other URL's to load?
Finally, I have an open source basic web browser for Cocoa that you can take a look at.
Basic Web Browser
I have one webview and 3 url.
So when application starts i am showing URL1 in webview.
Now when i select any portion of webview it will redirect to URL2.
But only i want to fetch some data from URL2 and dont want to show it to user.
Which i can able to do by using shouldStartLoadWithRequest: method with return NO.
but Now i need to show URL 3 with data received from URL2 in my Webview.
But it is not showing anything ,how can i do it ?
For this i am using following code
-(void)viewDidLoad
{
//Normal showing of URL1 in webview
}
- (BOOL)webView:(UIWebView*)webViewRef shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
{
if(selectedDataExist){
//get data from URL2
//Make New URL3 string
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:myNewUrlString]]];
return NO;
}
else
{
//by default URL1 comes
return YES;
}
i did this:
i was trying to send a info via GET method so i substring the last 10 characters (in my case) of any url requested, if it doesn't have the GET method, it make a new request appending the GET method to the url and return NO, next time this function get called it will have the GET method so it will continue.
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *urls = [request.URL absoluteString];
NSString *code = [urls substringFromIndex: [urls length] - 10];
if (![code isEqualToString:#"?iphone=si"]) {
NSURL *nueva = [NSURL URLWithString:[NSString stringWithFormat:#"%#?iphone=si",urls]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:nueva];
[self.mywebview loadRequest:requestObj];
return NO;
}
return YES;
}
This is how I do it
NSURL *LocalUrl = [[NSURL alloc] initWithString:[newUrl stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]];
NSURLRequest *objNSURLRequest;
objNSURLRequest = [NSURLRequest requestWithURL:LocalUrl];
[yourwebview loadRequest:ObjNSURLRequest];
[LocalUrl release];
return NO;
I use a WebViewController to visit some URL's in an app.
Now I found out that the apps size slowly increases every time using the WebViewController.
It is when checking app size under iPhone usage and the Document and Data for that specific app increases for every time it is used. The app can be 2 Mb when installed but after a lot of usage it may easily increase to 25 Mb. How can I release that fingerprint when closing the app??
When calling for the WebViewController this is how it looks.
- (void) showURL:(NSURL *)address withTitle:(NSString *)title
{
MyWebViewController *vc = [[MyWebViewController alloc] initWithNibName:#"MyWebViewController" bundle:nil];//ny
vc.title = title;
vc.location = address;
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
h. file in WebWiewController has this in its content.
NSURL *location;
IBOutlet UIWebView *theWebView;
and the m.file has this
- (void)viewDidLoad
{
[super viewDidLoad];
[theWebView loadRequest:[NSURLRequest requestWithURL:location]];
}
It seems that your Webview is caching the page.
Maybe you should manually create your http request to custom the cache policy.
for example :
NSURL *url = [NSURL URLWithString:URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[webView loadRequest:request];
I did not try the code, it's just to give an idea.
I want to open an iTunes link in my webView, but when the webView launches the page, it redirects to the Safari browser. There, the url is getting opened, but I want it to open in my webView.
- (void)viewDidLoad {
NSString *urlAddress = #"http://itunes.apple.com/us/app/foodcheck-traffic-light-nutrition/id386368933?mt=8";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
[super viewDidLoad];
}
Please suggest a way to sort out this problem.
You may want to try logging the load requests when you run the app. It may be that apple is automatically changing http:// to itms-apps or http://phobos or something along these lines. If so, then you can block the load when it's call using something like this:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *loadURL = [[request URL] retain];
NSLog(#"%#",loadURL);
if([[loadURL absoluteString] hasPrefix:#"http://"])
{
[loadURL release];
return TRUE;
}
[loadURL release];
return FALSE;
}
Good luck. I'm curious to know what finally works.
A note from the Apple reference documents- Q: How do I launch the App Store from my iPhone application? Also, how do I link to my application on the store?
Note: If you have iTunes links inside
a UIWebView, you can use this
technique after intercepting the links
with the -[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:]
delegate method.
Not sure if you ever got it working, but this works well for me:
NSString *responseString = [NSString stringWithContentsOfURL:myURL];
[self.webView loadHTMLString:responseString baseURL: nil)];
I have a WebView loading a Twitter mobile user's wall:
NSString *urlAddress = [NSString stringWithString:#"http://mobile.twitter.com/gatrecords"];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
When web is loaded it freezes for 10- 15 secs then I can navigate normally. On the iPhone's Safary Browser happens the same. That behavior only happens with loading mobile.twitter plus a user's wall.
On the simulator all is ok.
Any suggostions on what could be wrong ?
I found a workaround for the freezeing. Apparently WebView freezed bacause tries to execute twitter's javascript for 10 seconds, after that time the system will stop trying and the WebView will be scrollable. So I stopLoading only when twitter's web is being loaded:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *twitter = [NSString stringWithFormat:#"%#", [[myWebView request] URL]];
if ([twitter isEqualToString:#"http://mobile.twitter.com/gatrecords"]) {
[myWebView stopLoading];
}
}
Then I get an error alert after the 10 seconds regarding javascript did not load correctly, I fix that as follows:
- (void)webViewDidStartLoad:(UIWebView *)webView {
[myWebView stringByEvaluatingJavaScriptFromString:#"window.alert=null;"];
}