Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

urldecode in objective-c

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

like image 639
Chris Avatar asked Jan 14 '11 22:01

Chris


People also ask

What is Urldecode?

URLDECODE is a string manipulation function that manipulates CHARACTER string data.

What is Urldecode C#?

UrlDecode(String) Converts a string that has been encoded for transmission in a URL into a decoded string.

What is meant by Urlencode and Urldecode?

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.

What is Urlencode and Urldecode in PHP?

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.


4 Answers

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.

like image 61
BadPirate Avatar answered Oct 03 '22 18:10

BadPirate


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] 
like image 23
Jesse Naugher Avatar answered Oct 03 '22 17:10

Jesse Naugher


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
like image 32
Avi Levin Avatar answered Oct 03 '22 19:10

Avi Levin


- (NSString *)URLDecode:(NSString *)stringToDecode
{
    NSString *result = [stringToDecode stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return result;
}

That's it

like image 25
lee Avatar answered Oct 03 '22 19:10

lee