简体   繁体   English

UIWebview:在Safari中打开某些链接(不是全部)

[英]UIWebview: open certain links in safari (not all)

I have a webpage in uiwebview.. On this page are a couple of http:// links. 我在uiwebview中有一个网页。.在此页面上有几个http://链接。 One of them I want to have it opened in safari. 我想其中之一是在野生动物园中打开它。 The rest can open in UIWebview. 其余的可以在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; 我的想法是使网站的链接之一指向safari://blah.com,并将上面的代码更改为;

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

Hoping this would open the safari:// url in safari and the rest in UIWebview. 希望这将在safari中打开safari://网址,并在UIWebview中打开其余网址。 But no luck. 但是没有运气。 It seems like only the standard stuff (like http https tel mailto and sms) work here. 似乎只有标准的东西(例如http https tel mailto和sms)在这里起作用。 Any ideas how to get around this? 任何想法如何解决这个问题?

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. safari://链接无法打开的问题在于,它实际上不是有效的方案,需要先将其更改为http://。 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;
    }

} 

Hoping this would open the safari:// url in safari and the rest in UIWebview. 希望这将在safari中打开safari://网址,并在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 return s) 这段代码是不必要的(不需要保留该对象),并且会产生内存泄漏(遍历输入第一个if语句并return s的代码路径)

// Auto release

This comment is misleading as you are not using autorelease in your code. 由于您未在代码中使用autorelease ,因此此注释具有误导性。

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. 您的代码也是[[(([[[bit] hard]到])),带有(全部)[[这些]括号]和(空格)]请勿使用“代码”或“前置”标签来格式化代码- -使用“ 101 010”按钮进行格式化。 I've fixed it up a little bit for you 我为你修理了一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM