如何检查url字符串是否包含swift中的单词?

O-m*_*kar 3 string ios swift swift3

我试图在网络视图转发到本地文件,如果URL中不包含的话我想我尝试了很多东西包含rangeofstrings没有为我工作.

  if let url = "http://facebook.com/url/url"{

        if url.contains("facebook.com") || url.contains("nocontent") || url.contains("nointernet") || url.contains("paypal.com"){
            //Doing something here
            return true
        }else{

            let htmlFile = Bundle.main.path(forResource: "nocontent", ofType: "html")
            let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
            webView.loadHTMLString(html!, baseURL: nil)
        }
    }
Run Code Online (Sandbox Code Playgroud)

Boi*_*ime 15

这应该做的工作:

if let url = URL(string: "http://facebook.com/url/url") {

    if url.absoluteString.range(of: "facebook.com") != nil {

        return true
    }

    return false
}
Run Code Online (Sandbox Code Playgroud)


Iva*_*iuk 5

对我也适用:

if url.absoluteString.contains("facebook.com") {
    // do something here
}
Run Code Online (Sandbox Code Playgroud)