NSString *url = @"http://tw.news.yahoo.com/麵包魂東京吸睛-粉絲包圍導演-061222049.html";
NSLog(@"url: %@", url);
//ios9中采用下边方法转换
NSString *urlString = [url
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
//之前方法
NSString *urlString = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"encode url: %@", encodeUrl);
NSString *decodeUrl = [encodeUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"decode url: %@", decodeUrl);
結果如下:
url: http://tw.news.yahoo.com/麵包魂東京吸睛-粉絲包圍導演-061222049.html
encode url: http://tw.news.yahoo.com/%E9%BA%B5%E5%8C%85%E9%AD%82%E6%9D%B1%E4%BA%AC%E5%90%B8%E7%9D%9B-%E7%B2%89%E7%B5%B2%E5%8C%85%E5%9C%8D%E5%B0%8E%E6%BC%94-061222049.html
decode url: http://tw.news.yahoo.com/麵包魂東京吸睛-粉絲包圍導演-061222049.html