Force a WebView link to launch Safari?

前端 未结 5 2059
广开言路
广开言路 2020-12-04 08:28

I have a UIWebView embedded within an iPhone app of mine. I want to be able to have certain links within that webview open into the full Mobile Safari app (i.e. not my embed

5条回答
  •  余生分开走
    2020-12-04 09:07

    Swift version of Brad Larson's answer:

    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    
        var url: NSURL = request.URL!
        var isExternalLink: Bool = url.scheme == "http" || url.scheme == "https" || url.scheme == "mailto"
        if (isExternalLink && navigationType == UIWebViewNavigationType.LinkClicked) {
            return !UIApplication.sharedApplication().openURL(request.URL!)
        } else {
            return true
        }
    }
    

提交回复
热议问题