美文网首页
iOS - 9.0之后 URL中文转换

iOS - 9.0之后 URL中文转换

作者: 西半球_ | 来源:发表于2019-07-24 17:11 被阅读0次

由于url支持26个英文字母、数字和少数几个特殊字符,因此,对于url中包含非标准url的字符时,就需要对其进行编码。

URL编码(对中文处理)

//编码
NSString *testStr = @"这是中文";
NSString *logStr = [testStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSLog(@" 编码: %@ ",logStr);

  //打印
 编码: %E8%BF%99%E6%98%AF%E4%B8%AD%E6%96%87 

URL解码(处理成中文)


  NString * logStr2 = [logStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //这种废弃了 
       
//解码
NSString *logStr2 = [logStr stringByRemovingPercentEncoding];
NSLog(@" 解码: %@ ",logStr2);

 //打印
解码: 这是中文

相关文章

网友评论

      本文标题:iOS - 9.0之后 URL中文转换

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