Stunner
Stunner

Reputation: 12224

webViewDidStartLoad Request URL Not Set to URL Being Loaded

I am working on a browser that has back and forward buttons, that when tapped, should immediately display the URL of the page being loaded. Currently I do something like [webView goBack]; and then implement the delegate UIWebView method:

- (void)webViewDidStartLoad:(UIWebView *)aWebView {
  NSLog(@"began loading URL: %@", aWebView.request.URL);
}

But this doesn't result in the behavior I expect.

For example, if I start out at www.google.com and then navigate to www.aol.com and then press the back button causing the web view's goBack code to be called, the NSLog will output began loading URL: http://www.aol.com/ even though it should state that google.com is being loaded. The google page will load perfectly however in the web view. What I don't understand is why the URL isn't consistent with what is being loaded. On occasion the log will spit out that aol.com is being loaded, followed by google.com, but this only happens sometimes. Is this a bug with the SDK or am I missing something?

Thanks in advance.

Upvotes: 2

Views: 2868

Answers (3)

rjm226
rjm226

Reputation: 1203

try:

NSString *currentURL;
currentURL = [[NSString alloc] initWithFormat:@"%@", webView.request.mainDocumentURL];

Upvotes: 0

adali
adali

Reputation: 5977

try in this delegate method

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"Request Url:%d",request.URL.absoluteURL);
}

Upvotes: 3

Malek_Jundi
Malek_Jundi

Reputation: 6160

I suggest to use an array of string every time you load a new url you add it in the array and beside it define a global counter increase/deacrease based on back/forward click so you can get the url easy like this

NSString *urlString = [urlArray objectAtIndex:theCounterValue];

Upvotes: 0

Related Questions