I'm dealing with an urlencoded string in objective-c. Is there a foundation function that actually reverse the urlENCODING?
The string received is like: K%FChlschrank but should be after decoding Kühlschrank
URLDECODE is a string manipulation function that manipulates CHARACTER string data.
UrlDecode(String) Converts a string that has been encoded for transmission in a URL into a decoded string.
urlencode () is the function that can be used conveniently to encode a string before using in a query part of a URL. This is a convenient way for passing variables to the next page. urldecode() is the function that is used to decode the encoded string.
application/x-www-form-urlencoded type This is a type of encoding-decoding approach where the built-in PHP functions urlencode() and urldecode()are implemented to encode and decode the URL, respectively. This encoding will replace almost all the special characters other than (_), (-), and (.) in the given URL.
I made a quick category to help resolve this :)
@interface NSString (stringByDecodingURLFormat) - (NSString *)stringByDecodingURLFormat; @end @implementation NSString - (NSString *)stringByDecodingURLFormat { NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "]; result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; return result; } @end
Once defined, this quickly can handle an encoded string:
NSString *decodedString = [myString stringByDecodingURLFormat];
Plenty of other ways to implement.
I believe this is what you are looking for:
- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Return Value:
A new string made by replacing in the receiver all percent escapes with the matching characters as determined by the given encoding. It returns nil
if the transformation is not possible, for example, the percent escapes give a byte sequence not legal in encoding.
[source: Apple NSString Class Reference]
Apple has depreacted stringByReplacingPercentEscapesUsingEncoding:
since iOS9. Please use stringByRemovingPercentEncoding
.
The new method, Returns a new string made from the receiver by replacing all percent-encoded sequences with the matching UTF-8 characters.
Example:
NSString *encodedLink = @"hello%20world";
NSString *decodedUrl = [encodedLink stringByRemovingPercentEncoding];
NSLog (@"%@", decodedUrl);
Output:
hello world
- (NSString *)URLDecode:(NSString *)stringToDecode
{
NSString *result = [stringToDecode stringByReplacingOccurrencesOfString:@"+" withString:@" "];
result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return result;
}
That's it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With