1

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?

vincentzvegaz
  • 175
  • 1
  • 7
Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51
  • So, have you seen the UIAlertView? Put NSLog("Load url %@", urlString); before "if" clause – Injectios Sep 17 '13 at 09:49
  • Have you set the webView's delegate using: [homepage setDelegate:self] (or using the Interface Builder)? – gWiz Sep 17 '13 at 09:55
  • @gWiz, no.. I added this to the viewdidload: [_homepage setDelegate:self]; But it's giving me a warning: Sending 'FirstViewController *' to parameter of incompatible type 'id' – Joran Den Houting Sep 17 '13 at 10:04
  • 1
    You should change this: `@interface FirstViewController : UIViewController` to this: `@interface FirstViewController : UIViewController` to get rid of the warning and possibly fix your problem – gWiz Sep 17 '13 at 10:08

3 Answers3

3

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;
}
Imran
  • 1,715
  • 2
  • 20
  • 42
  • Thanks for your suggestion, but it's still not working.. Here is an example; - (BOOL)webView:(UIWebView *)homeview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *urlString = [[request URL] absoluteString]; if([urlString compare: @"http://www.sportdirect.com/order/cart" options: NSCaseInsensitiveSearch range: NSMakeRange(0, 37)] == NSOrderedSame) { // alert – Joran Den Houting Sep 17 '13 at 10:01
  • Can you sense the difference between the url you are comparing and the url you are getting.Print and check.May be some issue down there. – Imran Sep 17 '13 at 10:09
1

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".

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

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

Josshad
  • 894
  • 7
  • 14