30

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

Chris
  • 2,296
  • 4
  • 27
  • 46

6 Answers6

61

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.

BadPirate
  • 25,802
  • 10
  • 92
  • 123
  • 2
    I believe the NSString result, should actually be NSString* result. – christophercotton Apr 25 '11 at 22:46
  • 1
    Great. How do you change & into & though? or &rquote; into " . And all their kind? – user4951 Jun 05 '11 at 11:32
  • I wrote a similar category but ran in to an issue when I tried to decode a string @"abcjhjhdfjhafjakhfjaklfj12346890(*^$#@@@#$%^^ ........", it this method returns nil. It looks like stringByReplacingPercentEscapesUsingEncoding is causing some problems because of "Returns nil if the transformation is not possible (i.e. the percent escapes give a byte sequence not legal in the given encoding). " (from NSURL.h). So just a heads up for those that ran into this issue. – tony.tc.leung Nov 12 '14 at 21:37
  • Good answer, the same issue we got. Thanks a lot – Rubycon Jul 20 '15 at 12:59
17

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]
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Jesse Naugher
  • 9,780
  • 1
  • 41
  • 56
  • what encoding should I pass as parameter? tried NSUTF8StringEncoding already – Chris Jan 14 '11 at 22:14
  • um, i don't know what encoding that actually is, but there is a long list available in the documentation. – Jesse Naugher Jan 14 '11 at 22:15
  • 1
    okay, I was getting response from an NSURLRequest. Following line fixed the issure: NSString* deviceName = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] stringByReplacingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]; – Chris Jan 14 '11 at 22:17
  • 1
    You should use NSUTF8StringEncoding when you do the URL decoding step, since the encoded bytes are in UTF8 format, if they follow any recent specs (I don't have them to hand). – d11wtq Jan 15 '11 at 00:48
  • 2
    You'll also want to replace "+" with " " (space) *before* you do the percent escape replacements. – d11wtq Jan 15 '11 at 00:49
  • Thx for the hint d11wtq. Think is, due to a super crapping front end, that we have to deal with, the string encoding is plattform dependant. Stupid I know! – Chris Jan 16 '11 at 15:14
14

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
Avi Levin
  • 1,868
  • 23
  • 32
  • but this does not convert + symbols with " " spaces – veeresh kumbar Aug 18 '18 at 07:41
  • @veereshkumbar - this is not true, the `encodedLink` variable should be encoded. For example `hello world` should be encoded to `hello%20world`. In this case, when you get `hello%20world` as the encodedLink, the `decodedUrl` variable will be `hello world`. – Avi Levin Aug 21 '18 at 09:16
7
- (NSString *)URLDecode:(NSString *)stringToDecode
{
    NSString *result = [stringToDecode stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return result;
}

That's it

lee
  • 7,955
  • 8
  • 44
  • 60
3

According to W3Schools, URLs can only be sent over the Internet using the ASCII character set. For me this piece of code worked:

NSString *original = @"K%FChlschrank";

NSString *result2 = [original stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Doug
  • 1,643
  • 2
  • 13
  • 10
-1

For parmanent solution on iOS 9, use fallowing code

NSURL* link = [NSURL URLWithString:[url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
SerkanHocam
  • 536
  • 6
  • 18