Getting URL of UIWebView

I'm sort of new to Xcode, so forgive me in advance for any obvious wrong things that I might write.

I'm trying to write something a bit simple using the UIWebView. I've already made it load upon the app's loading, but I can't seem to do anything else. What I want to do next is to make a button appear/disappear depending on the current URL. This is what I used to (try to) get the current URL:

NSString *currentURL = viewWeb.request.URL.absoluteString;

(I'm using this code in the ViewController.h file)

When I made the outlet (Ctrl+dragging), I named it viewWeb and I also went and labeled it viewWeb. But it doesn't seem to work and I don't know why.

Also, please don't just give me some code without explaining, because this is a bit frustrating and I want to understand it.

EDIT: Thanks, but I'm not looking for help on the disappearing button just yet (by the way, viewWeb is my UIWebView, not the button). I need help to detect an URL change to make the button disappear. Is there a webViewDidLoad or something similar? viewDidLoad isn't for this.

Upvotes: 0

Views: 86

Answers (2)

DominicanCoder
DominicanCoder

Reputation: 365

In the ViewController.h File, you only declare the property, like this:

@property (strong,nonatomic) NSString *currentURL;

Then you can set the value in the ViewController.m file.

maybe in the -viewDidLoad method in the ViewController.m file like this:

_currentURL = viewWeb.request.URL.absoluteString;

Upvotes: 1

Dinesh
Dinesh

Reputation: 2204

You can set the property of hidden or not of the button to show it selectively based on the url.

// URLString contains the URL based on which you would like to show/hide the button
if ([currentURL isEqualToString:URLString]) {
    viewWeb.hidden = NO;
}
else {
    viewWeb.hidden = YES;
}

Upvotes: 0

Related Questions