user1131097
user1131097

Reputation: 7

problems showing the actual URL of a page in my app

I'm making an iPad browser, and I have a little problem. When I go to google, for example, I write the google direction in a textField. The problem is that when I change the web page, it still says http://www.google.com/. Here's my code:

-(IBAction)buttonpressed2:(id)sender {

    url = [NSURL URLWithString:[textField text]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    [webView isEqual:textField.text];
}

How can I make the textField show the page that I'm watching right now, and not the first url typed in the field?

Upvotes: 0

Views: 264

Answers (2)

Andrew
Andrew

Reputation: 3892

First, make sure when you set up the webView, you assign the UIWebViewDelegate to self

Then, implement the delegate method: webViewDidFinishLoad, as such:

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    NSURLRequest *currentRequest = [webView request];
    NSURL *currentURL = [currentRequest URL];

    //This will log the current url
    NSLog(@"Current URL is %@", currentURL.absoluteString);

    //Then to display it in the textField
    textfield.text = currentURL.absoluteString;
 }

Edit:

A delegate is a special set of methods that wait for an event to happen. When that event happens, the appropiate method is called. The method above is one of the delegate methods for a UIWebView - When a specific event happens with the webView, in this case when the webViewDidFinishLoad, that method is automtically called and we can respond as such. In order to set the delegate, we can proceed one of two ways:

  1. If you created the webView in code, this is with something like this:

    UIWebView *webView = [[UIWebView alloc] init...]
    

    then all you need to do is add the following line of code:

    webView.delegate = self;
    
  2. However, you might have also created the webView in the interface builder - If that's the case, click on the webView and the linker tab, and drag the delegate option over to "File's Owner".

and thats it.

Upvotes: 2

jonkroll
jonkroll

Reputation: 15722

Your class should implement the UIWebViewDelegate protocol. Then in the method webView:shouldStartLoadWithRequest:navigationType: you can do this:

[textField setText:[[request URL] absoluteString]];

This way the value in the textField will be updated whenever you navigate to a new url.

Upvotes: 0

Related Questions