iOS : How to do proper URL encoding?

后端 未结 10 1908
一整个雨季
一整个雨季 2020-12-13 19:20

I\'m unable to open a URL into UIWebView so I\'ve seached & found that I need to encode URL, so I tried to encode it but, I\'ve facing problem in URL encodi

10条回答
  •  情歌与酒
    2020-12-13 19:47

    The most straightforward way is to use:

    NSString *encodedString = [rawString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    

    iDhaval was close, but he was doing it the other way around (decoding instead of encoding).

    Anand's way would work, but you'll most likely have to replace more characters than spaces and new lines. See the reference here: http://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters

    Hope that helps.

提交回复
热议问题