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?

1

2 Answers 2

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];
}
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.

1
  • I don't want an URL, I want a NSString which is string encoded
    – adit
    Jul 14, 2011 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.