[英]How do i check if url string contains a word in swift?
我試圖在網絡視圖轉發到本地文件,如果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)
}
}
這應該可以完成這項工作:
if let url = URL(string: "http://facebook.com/url/url") {
if url.absoluteString.range(of: "facebook.com") != nil {
return true
}
return false
}
也適用於我:
if url.absoluteString.contains("facebook.com") {
// do something here
}
這對我有用
guard url.path.contains("facebook.com") else { return nil }
//do what you want
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.