【问题标题】:UIWebview: open certain links in safari (not all)UIWebview:在 safari 中打开某些链接(不是全部)
【发布时间】:2011-04-05 09:08:21
【问题描述】:

我在 uiwebview 中有一个网页。这个页面上有几个 http:// 链接。其中一个我想在 safari 中打开它。其余的可以在 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;  
} 

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

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

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

【问题讨论】:

  • 我知道我们将在这里进行两年,但是您会接受我的回答,因为它有很多赞成票并且实际上是解决方案吗?我可以使用我第一个接受的答案! :)

标签: iphone xcode uiwebview


【解决方案1】:

我需要做同样的事情,并使用上面的代码作为我的解决方案的起点。 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;
    }

} 

【讨论】:

    【解决方案2】:

    希望这会打开 safari:// Safari 中的 url 和其余的 UIWeb 视图。但没有运气。

    在这里你没有确切地说为什么它不起作用。您能否提供更多详细信息 - 您尝试了什么、您期望什么以及发生了什么?

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

    这段代码是不必要的(你不需要保留那个对象)并且会产生内存泄漏(遍历进入第一个if 语句和returns 的代码路径)

    // Auto release
    

    此评论具有误导性,因为您没有在代码中使用 autorelease

    您的代码也是带有 (all) [[那些] 括号] 和 (空格)] 的 [ ( ( [ [bit] hard ] to ) read ) 不要使用“code”或“pre”标签格式化代码——使用“101 010”按钮进行格式化。我已经为你修好了一点

    【讨论】:

    • 抱歉代码混乱.. 尝试了 3 个小时,但没有成功。当我说它不起作用时,我的意思是带有 http:// 的链接在 uiwebview 中打开,带有 safari:// 的链接在单击时不会做任何事情。我的目标是在 safari 中打开这些链接。
    • 那么带有 safari:// 的链接会触发这个方法吗?
    • 不,这种方法似乎只适用于 http: 和 https:.. 好像你不能像 safari:// 这样添加自己的类型
    猜你喜欢
    • 1970-01-01
    • 2015-12-02
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多