If UIWebview url is equal to

2019-08-06 03:01发布

问题:

I got a UIWebView called "homeview". Everytime someone is browsing the UIWebview I want to check if the page url is "http://www.website.com/cart" and then change the tab controller to the second tab.

So I did it like this:

- (BOOL)webView:(UIWebView *)homeview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *URLString = [[request URL] absoluteString];
if ([URLString isEqualToString:@"http://www.website.com/cart"]) {
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:2];
}
return YES;
}

But with no luck... So I tried to get an alert when page is equal to the url with:

- (BOOL)webView:(UIWebView *)homeview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *URLString = [[request URL] absoluteString];
if ([URLString isEqualToString:@"http://www.website.com/cart"]) {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Alert Title here"
                                                   message: @"Alert Message here"
                                                  delegate: self
                                         cancelButtonTitle:@"Cancel"
                                         otherButtonTitles:@"OK",nil];


    [alert show];
    [alert release];
}
return YES;
}

But that didn't the trick as well...

The above code is placed in my FirstViewController.m and this is my FirstViewController.h:

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
{
IBOutlet UIActivityIndicatorView *indicator;
IBOutlet UIWebView *homeview;
NSTimer *timer;
}
@property (retain, nonatomic) IBOutlet UIWebView *homepage;

@end

Anyone who can help me out?

回答1:

Try this:

Use *Compare:*to compare two strings.

- (BOOL)webView:(UIWebView *)homeview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *URLString = [[request URL] absoluteString];
if ([URLString Compare:@"http://www.website.com/cart"]==NSOrderedSame) {
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:2];
}
return YES;
}


回答2:

I suspect part of the problem is that you are looking for an exact match to the URL "http://www.website.com/cart" while the URL that's really being loaded by your web view is something like "http://www.website.com/cart?userid=123456&order=abcdefg&giftidea=something+expensive".

Your idea seems like a decent approach to me at first glance, but try changing this line:

if ([URLString isEqualToString:@"http://www.website.com/cart"]) {

to this:

if([URLString compare: @"http://www.website.com/cart" options: NSCaseInsensitiveSearch range: NSMakeRange(0, 27)] == NSOrderedSame)

and see if you have a happier result.

Also, for more readable code, Apple suggests that Objective-C variables and objects all start with lower case letters. So instead of "URLString", use "urlString" or something even more descriptive, like "urlAsString" or "urlStringFromWebView".



回答3:

If url loaded by UIWebView is not exactly equal to yours then you can check that location of your string isn't empty. F.e.:

    if ([[[request URL] absoluteString]
            rangeOfString:@"http://www.website.com/cart"].location != NSNotFound) { ... }

Or you can check the line @"www.website.com/cart" to take into account https protocol