Here’s a short snippet how to encode a string to a URL compatible string.
Don’t forget to decode it on the responder’s side.
- (NSString*)encodeString:(NSString*)string
{
NSString *encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
return encodedString;
}
The same with decoding:
- (NSString*)decodeString:(NSString*)string
{
NSString *decodedString = [string stringByRemovingPercentEncoding]
return decodedString;
}
Happy Coding!
原文链接:http://www.iossnippet.com/snippets/string/how-to-url-encode-decode-strings-nsstring-in-objective-c-ios/