开发者

URL decoding/encoding NSString

开发者 https://www.devze.com 2023-03-20 00:26 出处:网络
I am using three20\'s URL navigator and I want to create a map as follows: [map from:[Group class] name:@\"s开发者_运维百科how\" toURL:@\"tt://group/(gid)/(name)\"];

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

[map from:[Group class] name:@"s开发者_运维百科how" 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消