2013-07-22 25 views
3

是否有可能覆蓋UIWebView中的URL?例如,如果我想檢測tel:鏈接並將+1添加到數字的開頭(如果它尚未包含)。Objective-C - 覆蓋UIWebView中的URL?

在Android上我不喜歡這樣寫道:

public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       if (url.startsWith("tel:1")) { 
         Intent intent = new Intent(Intent.ACTION_DIAL, 
           Uri.parse(url)); 
         startActivity(intent); 
        } 
       else if (url.startsWith("tel:")) { 

        int start = url.indexOf(":"); 
        String suffix = url.substring(start + 1); 
        String newUrl = "tel:1" + suffix; 
         Intent intent = new Intent(Intent.ACTION_DIAL, 
           Uri.parse(newUrl)); 
         startActivity(intent); 
       } 

       else if(url.startsWith("http:") || url.startsWith("https:")) { 
        view.loadUrl(url); 
       } 
       return true; 
      } 

我怎樣才能做到這一點在Objective-C?

回答

6

您可以實施webView:shouldStartLoadWithRequest:navigationType:網絡視圖委託方法。檢查「點擊」類型,然後檢查請求的URL以查看它是否爲tel: URL。如果是這樣,您可以根據需要處理URL。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
    if (navigationType == UIWebViewNavigationTypeLinkClicked) { 
     NSURL *url = request.URL; 
     NSString *scheme = [url scheme]; 
     if ([scheme isEqualToString:@"tel"]) { 
      // Update the url as needed 

      // Now handled the url by asking the app to open it 
      [[UIApplication sharedApplication] openURL:url]; 
      return NO; // don't let the webview process it. 
     } 
    } 

    return YES; 
} 
+0

美麗。正是我需要的。謝謝! –