-2

I am trying to URL encode an NSString but I can't seem to find the proper way (I'm new to Obj C). I have been searching around but can't find a decent answer. So:

What is the correct, and modern, way to encode an NSString like a URL?

Thanks in advance.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
casparjespersen
  • 3,460
  • 5
  • 38
  • 63
  • possible duplicate of [URL encode a NSString](http://stackoverflow.com/questions/8088473/url-encode-a-nsstring), http://stackoverflow.com/questions/3423545/objective-c-iphone-percent-encode-a-string?lq=1, http://stackoverflow.com/questions/8086584/objective-c-url-encoding?lq=1, http://stackoverflow.com/questions/6688287/url-decoding-encoding-nsstring, http://stackoverflow.com/questions/9187316/string-wont-url-encode-in-ios – CodaFi Mar 03 '13 at 19:24
  • [NSString URLEncoding](http://madebymany.com/blog/url-encoding-an-nsstring-on-ios) – nsgulliver Mar 03 '13 at 19:25

1 Answers1

3

Encoding to UTF-8:

NSString *strUTF8 = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
ILYA2606
  • 587
  • 3
  • 7
  • Wrong answer. Translates "This is my + statement. Got it?" to "This%20is%20my%20+%20statement.%20Got%20it?" instead of "This+is+my+%2B+statement.+Got+it%3F". Note it doesn't properly encoded the plus and the question mark characters. – Volomike Jul 15 '16 at 07:00