Phil Hudson
Phil Hudson

Reputation: 3901

UIWebView to open URLs in Safari, but not URLs that start with a #

I've tried using the code below to open links within my HTML5 app in Safari. However the code is also opening # links in Safari that are intended for internal navigation within the app. Are the links being prepended with HTTP which is causing them to be opened in Safari? If so, how could I modify this script to exclude them?

Thanks.

FOR REFERENCE

Please see the GIT repo here: https://github.com/philhudson91/flaming-cyril

Or could I write it to stop links from the domain I'm hosting on from opening?

UPDATE

Here's the code that I am now using...

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *requestURL =[ [ request URL ] retain ];
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"#"] invertedSet];
if ([ [ requestURL scheme ] isEqualToString: @"http" ]) NSLog(@"HTTP"); if ([ [ requestURL scheme ] isEqualToString: @"https" ]) NSLog(@"HTTPS"); if (( [ [requestURL absoluteString] rangeOfCharacterFromSet:set].location == NSNotFound )) NSLog(@"Not Local"); if (( [ [ requestURL scheme ] isEqualToString: @"mailto" ])
    && ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
    return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
[ requestURL release ];
return YES; 
}

Upvotes: 2

Views: 1831

Answers (2)

Kiran
Kiran

Reputation: 5526

You can try the following: (I haven't tested, this just verifies URL doesn't contain '#' when request scheme is http or https)

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
  NSURL *requestURL =[ [ request URL ] retain ];
  NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"#"] invertedSet];

  NSLog([requestURL absoluteString]);

  if ( ((( ([ [ requestURL scheme ] isEqualToString: @"http" ]) || 
           ([ [ requestURL scheme ] isEqualToString: @"https" ])) && 
         ( [ [requestURL absoluteString] rangeOfCharacterFromSet:set].location != NSNotFound ) ) || 
        ( [ [ requestURL scheme ] isEqualToString: @"mailto" ]) ) && 
      ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
      return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
  }
  [ requestURL release ];
  return YES; 
}

Upvotes: 2

Andrei Chevozerov
Andrei Chevozerov

Reputation: 1029

Previous answer is quite fine but seems to have too many checks. I'd rather use something like that:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
    NSString *urlString = request.URL.absoluteString;

    if (([urlString rangeOfString:@"http://m.bu-news.com"].location != NSNotFound)) {
        return YES;
    }
    [[UIApplication sharedApplication] openURL:request.URL];
    return NO;
}

And it should be enough.

UPDATE: Did checkout your code and test it a bit. The code above works as expected: all navigation links opens inside app and all "Read full article" links opens in Safari. But didn't check all links, may be there are still some issues present.

Upvotes: 1

Related Questions