0

I have a webpage in uiwebview.. On this page are a couple of http:// links. One of them I want to have it opened in safari. The rest can open in UIWebview. I used this code so far;

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{  
    NSURL *requestURL = [ [ request URL ] retain ];  
    // Check to see what protocol/scheme the requested URL is.  
    if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ]  
    || [ [ requestURL scheme ] isEqualToString: @"https" ] )  
        && ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {  
        return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];  
    }  
    // Auto release  
    [ requestURL release ];  
    // If request url is something other than http or https it will open  
    // in UIWebView. You could also check for the other following  
    // protocols: tel, mailto and sms  
    return YES;  
} 

This works fine for the http and https etc. My idea was to make ONE of the links of the website point to safari://blah.com and change the above code to;

if ( ( [ [ requestURL scheme ] isEqualToString: @"safari" ]  
|| [ [ requestURL scheme ] isEqualToString: @"https" ] )  

Hoping this would open the safari:// url in safari and the rest in UIWebview. But no luck. It seems like only the standard stuff (like http https tel mailto and sms) work here. Any ideas how to get around this?

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
Alex van Es
  • 1,141
  • 2
  • 15
  • 27
  • I know we're going on two years here, but would you accept my answer as it has a number of upvotes and is in fact the solution? I could use my first accepted answer! :) – Andrew Hoyer Aug 14 '12 at 07:27

2 Answers2

4

I needed to do the same thing, and used the code above as a starting point for my solution. The problem of safari:// links not opening is that it's not really a valid scheme, and needs to be changed to http:// first. Hope this helps anyone who needs to do this.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {  

    // Make sure it's a link click that called this function.
    if ( navigationType == UIWebViewNavigationTypeLinkClicked ) {
        NSURL *requestURL = [ request URL ];  

        // Check to see what protocol/scheme the requested URL is.
        if ( [[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString: @"https"] ) { 

            // If it is HTTP or HTTPS, just return YES and the page loads.

            return YES;

        } else  {

            // Everything else loads here.  We assume what we're dealing with is safari://

            // It's important to replace safari:// with http:// or it won't load anyway
            [[ UIApplication sharedApplication ] openURL: [NSURL URLWithString: [[requestURL absoluteString] stringByReplacingOccurrencesOfString:@"safari://" withString:@"http://"] ]];
            return NO;

        }

    } else {
        return YES;
    }

} 
Andrew Hoyer
  • 801
  • 1
  • 8
  • 13
0

Hoping this would open the safari:// url in safari and the rest in UIWebview. But no luck.

Here you don't say exactly why it doesn't work. Can you provide more detail -- what did you try, what did you expect, and what happened instead?

NSURL *requestURL = [ [ request URL ] retain ];  

This code is unnecessary (you don't need to retain that object) and produces a memory leak (traveling the code path that enters the first if statement and returns)

// Auto release

This comment is misleading as you are not using autorelease in your code.

Your code also is also a [ ( ( [ [ bit ] hard ] to ) read ) with ( all ) [ [ those ] brackets ] and ( spaces ) ] Don't use "code" or "pre" tags to format code -- use the "101 010" button to format instead. I've fixed it up a little bit for you

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
  • Sorry about the messy code.. been trying for 3 hours, but no luck. When I say it doesn't work I mean the links with http:// get opened in uiwebview, the links with safari:// don't do anything when clicked. My goal is to have those links opened in safari. – Alex van Es Aug 29 '10 at 09:25
  • So do the links with safari:// trigger this method at all? – Shaggy Frog Aug 29 '10 at 19:22
  • No, this method only seems to work with http: and https:.. seems like you cannot add your own type like safari:// – Alex van Es Aug 30 '10 at 05:44