UIWebView only calls didFailLoadWithError once - iphone

I have a native iPhone app that just loads a website using UIWebView. I implemented the UIWebViewDelegate function didFailLoadWithError to display an error message when the user attempts to use the app without an Internet connection. The alert box shows up the first time I build and run the app, but does not appear thereafter. If I click the home button, then tap my app again, I just see a blank UIWebView. It's as if it's not trying to load the page again, so it never triggers didFailLoadWithError. This is just a simple app without saving and reloading of state, so shouldn't reloading the app re-run all my code, including the attempted loading of the website?
- (void)viewDidLoad {
NSString* urlString = [NSString stringWithString:#"http://www.site.com"];
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
[super viewDidLoad];
}
#pragma mark UIWebView delegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
//[myActivityIndicatorView stopAnimating];
NSLog([NSString stringWithFormat: #"%d", [error code]]);
if ([error code] != USER_STOPPED_LOAD) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Connection Error!"
message:#"You must be connected to the Internet to use Site."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}

No because it only gets sent to the background when you click the Home button! To restart the app, kill it using the multitasking bar.

Related

UIWebView does not load the URL

I have very small problem while loading the URL in UIWebView.
I have one UIButton, on clicking of it, I add the UIView which contains UIWebView, UIButton & Title bar. Code used is as follows -
[self.view addSubview:vwOnline];
vwOnline.bounds = self.view.bounds;
//Load webview
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#", objWine.strOnlineURL]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[wbvwOnline loadRequest:request];
- (void)webViewDidStartLoad:(UIWebView *)webView
{
//Show activity indicator
[indicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//Remove Activity indicator
[indicator stopAnimating];
[indicator removeFromSuperview];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"Error - %#", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Find A Vino" message:#"Error while loading request." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
alert.tag = 1;
//Remove Activity indicator
[indicator stopAnimating];
[indicator removeFromSuperview];
}
By doing above code, most of times the UIWebView does not loads the URL which I passed in the object objWine.strOnlineURL. If I clicked the back button & again clicked on the button to load the URL, it goes into the - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error delegate method of UIWebView.
If anyone knows the solution, then please help me.
Instead of adding your webview on your button click each time. Why don't you add your webview into your viewDidLoad and hide and show it in your button click.
when you are going back, make request to nil and load empty htmlstring to webview. One more thing, remove [indicator removeFromSuperview]; line from
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
-(void)webViewDidFinishLoad:(UIWebView *)webView
use
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.com"]];
if it is work fine then check objWine.strOnlineURL
otherwise it is clear :)

I want to load a webpage on login page .I've created a log-in page with a static ID and password.

I want to create a login page that will take login details of user and redirect him to the main page. i dont know how can i do that . plz help me.
if([username isEqualToString:#"aa"] && [password isEqualToString:#"aa"])
{
Supportingwv *swv = [[Supportingwv alloc]initWithNibName:#"supportingwv"bundle:nil];
NSString *urlstring = #"www.google.com";
[ swv setUrlString : urlstring];
[self.view addSubview:swv.view];
}
else
{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:#"invalid authentication" message:#"username & password doesnot match" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert show];
[alert show];
}
I guess Supportingwv is a UIWebView, so you have to load the request loadRequest method :
[swv loadRequest:[NSURLRequest requestWithURL:[NSURL urlWithString:urlString]]];
But without more explanation we can't help you.
EDIT :
If you want to load a page just use the following :
if([username isEqualToString:#"aa"] && [password isEqualToString:#"aa"])
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[webView loadRequest:[NSURLRequest requestWithURL:[URL urlWithString:#"www.google.com"]]];
[self.view addSubview:webView];
}
else
{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:#"invalid authentication" message:#"username & password doesnot match" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert show];
}
This code creates a webview with the URL you want (here it's www.google.com) and it add and show the webview to the current controller.
try this
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[swv loadRequest:requestObj];
[self.view addSubview:swv.view];
[swv release];
You should check now in this delegate of webview
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSString *urlString = request.URL.absoluteString;
//Check the username or password in urlString with the help of RegularExpression
//or check user has succssfully login or not
}

Message sent to deallocated instance in iPhone

I am new to iPhone,
I am creating NSURLConnection as suggested by apple here
but my app crashes when i Dismiss my view, i have tried concept of NSZombieEnabled which shows me -[CALayer release]: message sent to deallocated instance 0x68b8f40
I am displaying a webpage in Webview, When users clicks on download link in the webview then shouldStartLoadWithRequest method will be called inside this method i am creating NSURLConnection.
here is my code snippet,
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1
{
[receivedData appendData:data1];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[receivedData length]);
DirPath=[self applicationDocumentsDirectory];
NSLog(#"DirPath=%#",DirPath);
[receivedData writeToFile:DirPath atomically:YES];
UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:#"Download Complete !"
message:nil delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[Alert show];
[Alert release];
// release the connection, and the data object
[connection release];
[receivedData release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error1
{
[connection release];
[receivedData release];
// inform the user
NSLog(#"Connection failed! Error - %# %#",
[error1 localizedDescription],
[[error1 userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
Durl=[[url absoluteString]copy];
//Checking for Duplicate .FILE at downloaded path....
BOOL success =[[NSFileManager defaultManager] fileExistsAtPath:path];
lastPath=[[url lastPathComponent] copy];
if (success) //if duplicate file found...
{
UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:#"This FILE is already present in Library."
message:#"Do you want to Downlaod again ?" delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Yes",#"No",nil];
[Alert show];
[Alert release];
}
else //if duplicate file not found directly start download...
{
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:Durl]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
receivedData = [[NSMutableData data] retain];
} else {
NSLog(#"Inform the user that the connection failed.");
}
return YES;
}
Any help will be appreciated.
Make sure that in your implementation of dealloc you are resetting the delegate to nil. Another place for doing so would be viewWillDisappear:.
The reason for your app to crash / access a zombie is that the UIWebView instance will possibly be trying to call back the viewController even though it has already been deallocated. To prevent that, you have to set the delegate of that UIWebView back to nil before the viewController goes out of scope. That is a general issue when working with delegation prior to iOS5's ARC implementation. iOS5 finally offers weak references, those actually nil themselves once their instance is getting deallocated.
Example A:
- (void)dealloc
{
[...]
_webView.delegate = nil;
}
Example B:
- (void)viewWillDisappaer:(BOOL)animated
{
[super viewWillDisappaer:animated];
_webView.delegate = nil;
}
Edit:
After reading your question once again, I realised that the zombie is a UIView or a UIControl since the message is sent to a CALayer. Make sure your problem actually is related to the web view by temporarily removing all webView related code.
I think issue with this NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
You are creating an instance of NSURLConnection with delegate self, when you dismiss the view everything get deallocated on that view. But when the NSURLConnection tries to call it's delegate method crash will occur.
So you need to set the delegate of NSURLConnection to nil in your viewWillDisappear for doing this, you need to create object of NSURLConnection in interface.
#interface yourClass
{
NSURLConnection *theConnection;
}
#end
in .m
theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
- (void)viewWillDisappaer:(BOOL)animated
{
[super viewWillDisappaer:animated];
theConnection.delegate = nil;
}

How to detect java script in UIWebView

In a web page i have two select box, after select a value from first select box the second select box if fill by java script or may be Ajex. but in my UIWebView the second select box is not fill. so please help me. this is work well in safari web browser.
- (void)viewDidLoad {
[super viewDidLoad];
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
}
-(void)webViewDidStartLoad:(UIWebView *) portal {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)webViewDidFinishLoad:(UIWebView *) portal{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSString *userInfo = [[error userInfo] objectForKey:NSURLErrorFailingURLPeerTrustErrorKey];
NSLog(#"Connection failed! Error - %# %#",[error localizedDescription],userInfo);
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:[error localizedDescription] message:userInfo delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil,nil];
[alerView show];
[alerView release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
You should turn on the Debug Console in your mobile safari and look if there java script errors, else you should debug through with alerts.
Turn on Debug Console:
Open Settings -> Safari -> Developer (At the bottom) -> Debug Console on|off

XCode webview connection/server error handling

All,
I am fairly new to XCode and am trying to get a handle on how to best deal with connection issues when trying to use a WebView. I know there are related questions on SO, but none seem to offer complete solutions. I have the following code, but it seems a little inefficient. Hopefully, someone can help me refactor it down to a point where it can be usable anywhere that a UIWebView is called.
NOTE: Please ignore memory issues for now. I realize that has to be added as well.
- (void)viewDidLoad {
[webView setDelegate:self];
NSString *urlAddress = #"http://www.somesite.com/somepage";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
[super viewDidLoad];
}
// Check for URLConnection failure
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:#"Connection error" message:#"Error connecting to page. Please check your 3G and/or Wifi settings." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[connectionError show];
webView.hidden = true;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
//Check for server error
if ([httpResponse statusCode] >= 400) {
UIAlertView *serverError = [[UIAlertView alloc] initWithTitle:#"Server error" message:#"Error connecting to page. If error persists, please contact support." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[serverError show];
webView.hidden = true;
//Otherwise load webView
} else {
// Redundant code
NSString *urlAddress = #"http://somesite.com/somepage";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
webView.hidden = false;
}
}
// Seems redundant since we are already checking the URLConnection
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:#"Connection error" message:#"Error connecting to page. Please check your 3G and/or Wifi settings." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[connectionError show];
}
I guess what I'm wondering is, are there any shortcuts to achieve the desired functionality? Can I somehow access the URLResponse via the WebView directly? Does a nil value for the URLConnection or UIWebView imply connection errors without having to explicitly check for them? Is there an easier way to pass the URLRequest down into the delegate methods so it doesn't have be recreated twice?
Thanks in advance!
- (void)viewDidLoad {
webView = [[UIWebView alloc]init];
[webView setDelegate:self];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://yourUrl.com"]]]
[super viewDidLoad];
}
#pragma mark UIWebView delegate methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//read your request here
//before the webview will load your request
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
//access your request
webView.request;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//access your request
webView.request;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(#"could not load the website caused by error: %#", error);
}
-(void)dealloc{
[webView release];
[super dealloc];
}
You can load the webpage with NSURLRequest directly into your webview. With the delegate callback methods you can change the behavior of your webview.
With this piece of code you should get it done.