美文网首页
URL 中文转码

URL 中文转码

作者: afakee | 来源:发表于2016-04-01 10:16 被阅读0次

比如: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/---

相关文章

网友评论

      本文标题:URL 中文转码

      本文链接:https://www.haomeiwen.com/subject/pthylttx.html