Search code examples
objective-cnsstringnsdatansurl

Convert URL to correct format


I have been trying to fix this for the past few hours and I cannot seem to get anything working. My question is straightforward, I need to convert some strings that are being inputted to a URL so that it correctly sends the spaces and special characters to the receiving server.

I have 3 strings: setFirstLast.text, emailAddress.text, and visitingTest.text. I need to convert them into something that prints like: John%20Smith instead of John Smith.


Solution

  • You need to use

    [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

    to get the desired result. string is the object containing your original text and NSUTF8StringEncoding is one of many encoding options available.