02-08-2012, 12:31 PM
#1 (permalink )
Registered Member
Join Date: Apr 2011
Posts: 39
UIWebView Refresh help (nested if-else)
I know I'm really close to solving the issue I'm having. The problem is that if there wasn't already a completed request (no internet), the webview won't reload the original link once the internet is reestablished (requestObj) Here is my code:
Code:
-(IBAction)refreshWebView {
NSString *onlineCheck = [[NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://www.google.com/favicon.ico"]] retain];
if (onlineCheck == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't connect to the server, check your connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
[onlineCheck release];
}
else {
if (webView.request != nil) {
[webView reload]; }
else {NSString *urlAddress = @"http://theverge.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj]; } }
NSLog(@"REFRESH");
toolbarStop.hidden = NO;
toolbarRefresh.hidden = YES;
}
Last edited by troop231; 02-08-2012 at 12:39 PM .
02-08-2012, 02:22 PM
#2 (permalink )
Registered Member
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
its hard to tell what you are trying to do or what the problems is, but one small issue is this line:
[onlineCheck release];
is in the wrong place, because it will get released some of the time but not always. better to make the string autorelease or move the release statement outside of the if statement.
also I would put your NSURLRequest code in a seperate function and call it when needed. It might help make the code more readable and allow you to solve your nested if/then issue.
Most likely you simply need a new if/then statement that is not inside another if/then/else statement.
02-08-2012, 02:27 PM
#3 (permalink )
Registered Member
Join Date: Apr 2011
Posts: 39
Quote:
Originally Posted by
RickSDK
its hard to tell what you are trying to do or what the problems is, but one small issue is this line:
[onlineCheck release];
is in the wrong place, because it will get released some of the time but not always. better to make the string autorelease or move the release statement outside of the if statement.
also I would put your NSURLRequest code in a seperate function and call it when needed. It might help make the code more readable and allow you to solve your nested if/then issue.
Most likely you simply need a new if/then statement that is not inside another if/then/else statement.
I'm a bit lost now, I probably should've posted all of my main code, so here it is (again, sorry, im not really sure what is messed up):
Code:
#import "Test.h"
@implementation Test
@synthesize webView;
@synthesize activityIndicator;
@synthesize refreshButton;
@synthesize backButton;
@synthesize forwardButton;
@synthesize backButtonT;
@synthesize forwardButtonT;
@synthesize stopButton;
@synthesize toolbarStop;
@synthesize toolbarRefresh;
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation !=
UIInterfaceOrientationPortraitUpsideDown);
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Test";
[webView addSubview: activityIndicator];
NSString *urlAddress = @"http://theverge.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSLog(@"hmm"); }
-(void)actualizeButtons {
backButton.enabled = [webView canGoBack];
forwardButton.enabled = [webView canGoForward];
backButtonT.enabled = [webView canGoBack];
forwardButtonT.enabled = [webView canGoForward];
NSLog(@"ACTUALIZE");
}
-(IBAction)refreshWebView {
NSString *onlineCheck = [[NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://www.google.com/favicon.ico"]] retain];
if (onlineCheck == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't connect to the internet, check your connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
[onlineCheck release];
}
else {
if (webView.request != nil) {
[webView reload]; }
else {NSString *urlAddress = @"http://theverge.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj]; } }
NSLog(@"REFRESH");
toolbarStop.hidden = YES;
toolbarRefresh.hidden = NO;
}
-(IBAction)stop {
NSLog(@"STOP");
toolbarStop.hidden = YES;
toolbarRefresh.hidden = NO;
[webView stopLoading];
}
-(IBAction)goBack {
NSLog(@"BACK");
[webView goBack];
[self actualizeButtons];
}
-(IBAction)goForward {
NSLog(@"FORWARD");
[webView goForward];
[self actualizeButtons];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(@"ERROR");
[activityIndicator stopAnimating];
toolbarStop.hidden = YES;
toolbarRefresh.hidden = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"FINISH LOAD");
[activityIndicator stopAnimating];
toolbarStop.hidden = YES;
toolbarRefresh.hidden = NO;
[self actualizeButtons];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSString *onlineCheck = [[NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://www.google.com/favicon.ico"]] retain];
if (onlineCheck == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't connect to the internet, check your connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
[onlineCheck release];
NSLog(@"UIAlert2");
}
[activityIndicator startAnimating];
toolbarStop.hidden = NO;
toolbarRefresh.hidden = YES;
NSLog(@"START LOAD");
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[refreshButton release];
[backButton release];
[forwardButton release];
[backButtonT release];
[forwardButtonT release];
[stopButton release];
[activityIndicator release];
[toolbarStop release];
[toolbarRefresh release];
[webView release];
}
@end
02-08-2012, 03:39 PM
#4 (permalink )
Registered Member
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
hmm. posting more code does not help.
I would start by doing to two things i mentioned in the previous post.
Then if you are still having problems, spell out exactly what is wrong and what is happening and why its bad to happen that way.
02-08-2012, 09:16 PM
#5 (permalink )
Registered Member
Join Date: Apr 2011
Posts: 39
Edit: I solved the problem, thanks again!
Last edited by troop231; 02-08-2012 at 09:47 PM .
02-08-2012, 09:52 PM
#6 (permalink )
Registered Member
Join Date: Aug 2011
Posts: 38
Quote:
Originally Posted by
troop231
Edit: I solved the problem, thanks again!
how you solve the problem? Can you share your solution?
02-08-2012, 10:50 PM
#7 (permalink )
Registered Member
Join Date: Apr 2011
Posts: 39
Quote:
Originally Posted by
cthesky
how you solve the problem? Can you share your solution?
Sure! (I'm not sure if this is clean code or not, but I tested it many times on the simulator AND iPhone, and it works perfectly)
I had to change the -(IBAction)refreshWebView to this:
Code:
-(IBAction)refreshWebView {
NSString *onlineCheck = [[NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://www.google.com/favicon.ico"]] retain];
if (onlineCheck == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't connect to the internet, check your connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
NSLog(@"UIAlert1");
}
else {
NSString *urlAddress = @"http://theverge.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj]; }
if (onlineCheck != nil) { [webView reload]; }
[onlineCheck release];
NSLog(@"REFRESH");
toolbarStop.hidden = YES;
toolbarRefresh.hidden = NO;
}
If you have any more questions, please feel free to ask!
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 388
13 members and 375 guests
7twenty7 , chiataytuday , fiftysixty , gmarro , iOS.Lover , KennyChong , kilobytedump , Leslie80 , Matrix23 , ryantcb , stanny , xerohuang , Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K