Xcode, Swift; Detect hyperlink click in UIWebView

前端 未结 3 1209
予麋鹿
予麋鹿 2021-01-13 06:57

I made an iOS app with Xcode and Swift.

I want to open specific hyperlinks in Safari browser, there others in the WebView itself.

To reach this, I\'ll have t

3条回答
  •  感情败类
    2021-01-13 07:07

    This is the working solution (Xcode 7.3.1 and Swift 2.2):

    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    
        if request.URL?.query?.rangeOfString("target=external") != nil {
            if let url = request.URL {
                UIApplication.sharedApplication().openURL(url)
                return false
            }
        }
        return true
    }
    

    Many thanks to danhhgl from freelancer.com!

提交回复
热议问题