繁体   English   中英

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

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

我在uiwebview中有一个网页。.在此页面上有几个http://链接。 我想其中之一是在野生动物园中打开它。 其余的可以在UIWebview中打开。 到目前为止,我一直使用此代码。

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

我的想法是使网站的链接之一指向safari://blah.com,并将上面的代码更改为;

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

希望这将在safari中打开safari://网址,并在UIWebview中打开其余网址。 但是没有运气。 似乎只有标准的东西(例如http https tel mailto和sms)在这里起作用。 任何想法如何解决这个问题?

我需要做同样的事情,并将上面的代码用作解决方案的起点。 safari://链接无法打开的问题在于,它实际上不是有效的方案,需要先将其更改为http://。 希望这对需要这样做的人有所帮助。

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

} 

希望这将在safari中打开safari://网址,并在UIWebview中打开其余网址。 但是没有运气。

在这里,您无法确切说明为什么它不起作用。 您能否提供更多细节-您尝试了什么,期望了什么以及发生了什么?

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

这段代码是不必要的(不需要保留该对象),并且会产生内存泄漏(遍历输入第一个if语句并return s的代码路径)

// Auto release

由于您未在代码中使用autorelease ,因此此注释具有误导性。

您的代码也是[[(([[[bit] hard]到])),带有(全部)[[这些]括号]和(空格)]请勿使用“代码”或“前置”标签来格式化代码- -使用“ 101 010”按钮进行格式化。 我为你修理了一点

暂无
暂无

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

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