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;"];
}
Related
I have a webView which will display the https url. I am trying to open a https url which will provide a authentication dialog like this http://members.easynews.com/ . In Mac, it is opening as dialog box, even in iPhone safari browser it is displaying as dialog box. But in webview , it is not displaying the dialog box. How to display that dialog box in webView ??
I think it is because of some firewall settings by that link authorities.thats why they are asking for authorisation.i am trying to load that in my webview i wont get anything .it is not loading
go for this
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://members.easynews.com"]];
instead of loading it in to webview
or else are loading i this manner
- (void)viewDidLoad {
[super viewDidLoad];
webView.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
}
add this new method in your class
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog("Error : %#",error);
}
I hope you have connected webView object with its outlet in Interface builder?
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];
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 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)];
The problem is that the website doesn't fully load in a UIWebView, but it loads normally in Safari.
This is the error I get when loading the website in a UIWebView:
Warning:
cos_before_render(/home/user/ctown/doc_wbn//../sys/swt/www.westspringsec.moe.edu.sg.mob)
[function.cos-before-render]: failed
to open stream: No such file or
directory in
/home/user/ctown/cti_bin/wbn/cos_init.inc
on line 731
Warning: cos_before_render()
[function.include]: Failed opening
'/home/user/ctown/doc_wbn//../sys/swt/www.westspringsec.moe.edu.sg.mob'
for inclusion
(include_path='.:/home/user/ctown/cti_bin/phplot:/usr/local/lib/php')
in
/home/user/ctown/cti_bin/wbn/cos_init.inc
on line 731
Fatal error: Call to undefined
function: json_encode() in
/home/user/ctown/cti_bin/wbn/cos_init.inc
on line 737
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.westspringsec.moe.edu.sg";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[Webview loadRequest:requestObj];
[super viewDidLoad];
}
I tried the question linked below, however I can't modify the backend of the website!
UIWebView Xhmtl parse error but safari don't
The error is not within your code! The UIWebView, especially from within the simulator uses a user agent something like this:
Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8C134
When you call the url provided it results in the error you are seeing. It's a server side error.
You can alter your user agent by using this code, put it somewhere in the startup phase of your app so it's only set once:
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:#"Safari/528.16", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
[dictionnary release];
With this I was able to open your page. It looks like the site returns different markup for the iphone, so the result with the code above will show the website, which is a bit large for the small display.
UIWebView and Safari does not have the same user agent.
This answer won't fix your issue (see Nick's answer), but you should put [super viewDidLoad]; before you do anything else. So:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = #"http://www.westspringsec.moe.edu.sg";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[Webview loadRequest:requestObj];
}