SWIFT: Why is “NSURL(string:” returning with Nil, even though it's a valid url in a browser?

爷,独闯天下 提交于 2019-12-01 05:50:02

You should URL-encode the URL like this:

selectedFeedUrl = selectedFeedUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

jobima

In iOs 9:

myString = myString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!

Hope it helps

For swift3 you can do like this

let url = URL(string:url.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)!

Thank you guys for your help. I added these two lines to my code and it works now:

    selectedFeedURL = selectedFeedURL.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
    selectedFeedURL =  selectedFeedURL.stringByReplacingOccurrencesOfString("%09%09", withString:"")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!