3

是否可以覆盖 UIWebView 中的 URL?例如,如果我想检测 tel: 链接并将 +1 添加到数字的开头(如果它还没有)。

在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 中做到这一点?

4

1 回答 1

6

您可以实现webView:shouldStartLoadWithRequest:navigationType:Web 视图委托方法。检查“点击”的类型,然后检查请求的 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;
}
于 2013-07-22T22:21:45.897 回答