0

I am using three20's URL navigator and I want to create a map as follows:

[map from:[Group class] name:@"show" toURL:@"tt://group/(gid)/(name)"];

The issue here name can be multiple words and so there is spaces in between. Now I need to URL encode this NSString and decode it back. How do I do this? What is the easiest way to URL decode and encode NSString?

pablasso
  • 2,479
  • 2
  • 26
  • 32
adit
  • 32,574
  • 72
  • 229
  • 373
  • possible duplicate of [Objective-c iPhone percent encode a string?](http://stackoverflow.com/questions/3423545/objective-c-iphone-percent-encode-a-string) – benzado Jul 14 '11 at 03:54

2 Answers2

3
- (NSString *)encodedURLString {
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)self,
                                                                           NULL,                   
                                                                           CFSTR("?=&+"),          
                                                                           kCFStringEncodingUTF8); // encoding
    return [result autorelease];
}

- (NSString *)encodedURLParameterString {
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                           (CFStringRef)self,
                                                                           NULL,
                                                                           CFSTR(":/=,!$&'()*+;[]@#?"),
                                                                           kCFStringEncodingUTF8);
    return [result autorelease];
}
pablasso
  • 2,479
  • 2
  • 26
  • 32
jing
  • 68
  • 4
0

You can start with

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];

to get the url from the NSString.

Also, see here about URLencoding it with the proper escapes.

Community
  • 1
  • 1
Chris Gregg
  • 2,376
  • 16
  • 30