码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 含有中文的URL转码问题

时间:2015-12-10 11:08:33      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:

非ARC模式下:

- (NSString *)encodeToPercentEscapeString: (NSString *) input
{
    NSString *outputStr = (NSString *) 
    CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                            (CFStringRef)input,
                                            NULL,
                                            (CFStringRef)@"!*‘();:@&=+$,/?%#[]",
                                            kCFStringEncodingUTF8);
    return outputStr;
}

- (NSString *)decodeFromPercentEscapeString: (NSString *) input
{
    NSMutableString *outputStr = [NSMutableString stringWithString:input];
    [outputStr replaceOccurrencesOfString:@"+"
                               withString:@" "
                                  options:NSLiteralSearch
                                    range:NSMakeRange(0, [outputStr length])];

    return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

 

ARC模式下:

- (NSString *)encodeToPercentEscapeString: (NSString *) input
{
    NSString*  outputStr = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                        NULL, /* allocator */
                                                                                        (__bridge CFStringRef)input,
                                                                                        NULL, /*解决含有的转义字符*/
                                                                                        (CFStringRef)@"!*‘();:@&=+$,/?%#[]",
                                                                                        kCFStringEncodingUTF8);
    return outputStr;
}
- (NSString *)decodeFromPercentEscapeString: (NSString *) input
{
    NSMutableString *outputStr = [NSMutableString stringWithString:input];
    [outputStr replaceOccurrencesOfString:@"+"
                withString:@""
                options:NSLiteralSearch
                range:NSMakeRange(0,
                [outputStr length])];

    return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

 

iOS 含有中文的URL转码问题

标签:

原文地址:http://www.cnblogs.com/ficow/p/5035075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!