In iOS8 and prior I can use:
NSString *str = ...; // some URL
NSString *result = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
What is the meaning of "This method is intended to percent-encode an URL component or subcomponent string, NOT an entire URL string." ? – GeneCode Sep 1 '16 at 8:30
It means that you are not supposed to encode the https://xpto.example.com/path/subpath
of the url, but only what goes after the ?
.
Supposed, because there are use-cases for doing it in cases like:
https://example.com?redirectme=xxxxx
Where xxxxx
is a fully encoded URL.
Objective-C
this code work for me :
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];