NSURL is returning nil for a valid URL

前端 未结 2 1108
一整个雨季
一整个雨季 2021-01-22 03:19

I have a valid URL for google maps , which if you run in your browser would show an image of a map. However, when I put it inside my Swift code and try to create an NSURL from S

2条回答
  •  [愿得一人]
    2021-01-22 03:53

    It should work: Here is why http://www.url-encode-decode.com

            let url = NSURL(string: urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
    

    Since above API is deprecated alternative approach is

        let url = NSURL(string: urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)
    

提交回复
热议问题