iOS 将网址中%形式的参数进行UTF8编码

2016-06-03  本文已影响124人  Joker_King

这个链接中%形式的参数转换成中文

我们需要使用如下的方法

NSString *string_1 = [@"https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=%E6%88%91%E4%BB%AC"
stringByRemovingPercentEncoding];
    NSLog(@"%@",string_1);

将链接中的中文转换成%形式

方法一:

NSString *string_2 = [@"https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=我们"
stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet characterSetWithCharactersInString:@"我们"].invertedSet];
NSLog(@"%@",string_2);

方法二:

NSString *urlStr = @"http://api.douban.com/v2/movie/nowplaying?app_name=doubanmovie&client=e:iPhone4,1|y:iPhoneOS_6.1|s:mobile|f:doubanmovie_2|v:3.3.1|m:PP_market|udid:aa1b815b8a4d1e961347304e74b9f9593d95e1c5&alt=json&version=2&start=0&city=北京&apikey=0df993c66c0c636e29ecbb5344252a4a";
NSString *urlEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
上一篇下一篇

猜你喜欢

热点阅读