UIWebview:在 safari 中打开某些链接(不是全部) [英] UIWebview: open certain links in safari (not all)

查看:34
本文介绍了UIWebview:在 safari 中打开某些链接(不是全部)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 uiwebview 中有一个网页.在这个页面上有几个 http://链接.其中之一我想在 safari 中打开它.其余的可以在 UIWebview 中打开.到目前为止,我使用了这段代码;

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;  
} 

这适用于 http 和 https 等.我的想法是让网站的链接之一指向 safari://blah.com 并将上述代码更改为;

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" ] )  

希望这会在 safari 中打开 safari://url,而在 UIWebview 中打开其余部分.但没有运气.似乎只有标准的东西(比如 http https tel mailto 和 sms)在这里工作.任何想法如何解决这个问题?

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?

推荐答案

我需要做同样的事情,并使用上面的代码作为我解决方案的起点.safari://链接打不开的问题是不是真正有效的方案,需要先改成http://.希望这可以帮助任何需要这样做的人.

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;
    }

} 

这篇关于UIWebview:在 safari 中打开某些链接(不是全部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆