URLString中文字符转义

 1     NSString *urlString = @"https://www.baidu.com?userName=中国";
 2     
 3     /*
 4      url中不能有中文、空格、特殊字符,否则NSURL为nil
 5      URLQueryAllowedCharacterSet:对查询字符串进行百分号转义
 6      */
 7     urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
 8     
 9     NSLog(@"%@",urlString);
10     
11     NSURL *url = [NSURL URLWithString:urlString];
12     
13     NSLog(@"%@",url);

 注:POST请求的查询字符串是不需要转义的,因为请求体是二进制格式的。

原文地址:https://www.cnblogs.com/panda1024/p/6284958.html