Henry F
Henry F

Reputation: 4980

Hiding A Button Based on Exact URL

I'm trying to hide a button only when a certain URL is being displayed in a UIWebView. This is the code I'm using to do so:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request {
    NSString *host = [request.URL host];
    if ([host isEqualToString:@"http://example.com/cheese"]) {
        _backButton.hidden = YES;
    } else {
        _backButton.hidden = NO;
    }
    return 0;
}

When I test it to see if the back button is hidden, it never is. It stays in plain view whether "example.com/cheese" is loaded or not. (That /cheese part is pretty important as well, maybe it is only checking the host and not the full URL?)

Either way, I have no idea what to change or add to get this working. Any help is greatly appreciated!

Update: I added in an NSLog just before the if statement, and it's not even firing. I also changed the code to the answer below (thanks again rmaddy). I have no idea why this method is not firing.

Upvotes: 0

Views: 62

Answers (1)

rmaddy
rmaddy

Reputation: 318824

The host part of the URL is just that, the host. If the URL is:

http://example.com/cheese

then the host is just example.com.

If you want to compare the full URL then do this:

NSString *full = [request.URL absoluteString];
if ([full isEqualToString:@"http://example.com/cheese"]) {
}

If you want to compare just the host then do this:

NSString *host = [request.URL host];
if ([host isEqualToString:@"example.com"]) {
}

What's odd is that you have it correct in the second bit of code you posted. It is wrong in the first bit of code you posted.

Also, in the first block of code, you call return 0 at the end. You should return either YES or NO since the return type is BOOL.

Upvotes: 1

Related Questions