簡體   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