3

I'm trying to Decode URL,but i'm getting warning message like "NSString may not respond to URLDecode".

NSString* token = [[urlStr substringFromIndex:r.location + 1] URLDecode];

Any ideas?

mttrb
  • 8,297
  • 3
  • 35
  • 57

2 Answers2

3

Because it is not a method a classic NSString respond to, you can though add a category like the following, to add the method yourself :

@interface NSString (URLDecode)
- (NSString *)URLDecode;
@end

@implementation NSString
- (NSString *)URLDecode
{
    NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return result;
}
@end
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • 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:35
1

Try like this.

NSString* urlEncoded = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

and check this link https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/TP40003744

it may help you.

Ganesh G
  • 1,991
  • 1
  • 24
  • 37