Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space in query string of a URL Making it nil

Tags:

ios

xcode9

swift3

I am working on a Swift 3.0 app, in which I am making a rest api call. The problem is that there may be a space in query string parameter, which is causing the URL to become nil. It works fine when there is no space in that parameter. But with a space it does not work.

Any help is appreciated.

Thanks

like image 828
Sundeep Saluja Avatar asked Jan 03 '23 16:01

Sundeep Saluja


1 Answers

You could do something like this to escape the spaces and other characters which may interfere with the request processing:

var escapedAddress = address.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())

For Swift 3, use the following:

let escapedAddress = address.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
like image 175
userx Avatar answered Jan 09 '23 05:01

userx