URL 中文转码

2016-04-01  本文已影响0人  afakee

比如:http://google.com/中国/
直接转 URL 会失败

NSString *stringURL = @"http://google.com/中国/";
NSLog(@"%@", [NSURL URLWithString:stringURL]);
----打印:(null)----

这时候需要对字符串进行转码处理

NSString *stringURL = [@"http://google.com/中国/" 
    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", [NSURL URLWithString:stringURL]);
---输出:http://google.com/%E4%B8%AD%E5%9B%BD/---
上一篇 下一篇

猜你喜欢

热点阅读