Swift. URL returning nil
I think this will help. Your element.name may contain space between words so addingPercentEncoding make PercentEncoding string.
let txtAppend = (element.Name).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let url = "http://en.wikipedia.org/wiki/\(txtAppend!)"
let openUrl = NSURL(string: url)
if #available(iOS 10.0, *) {
UIApplication.shared.open(openUrl as! URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(openUrl as! URL)
}
Swift 4 version for this fix:
str.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)