关于 iOS 不受支持的 URL 问题
https://blog.csdn.net/huanghuipost/article/details/102521074
https://www.cnblogs.com/thinksasa/p/3530813.html
https://www.cnblogs.com/huzi007/p/4174519.html
https://blog.csdn.net/weixin_33901926/article/details/89039096
第一:
URL中含有转义字符例如 双引号 "" 、大括号{}等
用stringByAddingPercentEncodingWithAllowedCharacters取代CFURLCreateStringByAddingPercentEscapes
1.网络访问请求:中文空格字符编码/解码
在 iOS 程序访问 HTTP 资源时需要对 URL 进行 Encode,
比如像拼出来的 http://unmi.cc?p1=%+&sd f&p2=中文,
其中的中文、特殊符号&%和空格都必须进行转译才能正确访问。
现在以“?!@#$^&%*+,:;='\”`<>()[]{}/\| "字符串为例子,
用stringByAddingPercentEncodingWithAllowedCharacters取代
CFURLCreateStringByAddingPercentEscapes
stringByAddingPercentEscapesUsingEncoding
(只对 `#%^{}[]|"<> 加空格共14个字符编码,不包括“&?”等符号),
iOS 9将淘汰,建议用stringByAddingPercentEncodingWithAllowedCharacters方法
URLUserAllowedCharacterSet "#%/:<>?@[\]^`
URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|}
URLHostAllowedCharacterSet "#%/<>?@\^`{|}
URLPathAllowedCharacterSet "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet "#%<>[\]^`{|}
URLFragmentAllowedCharacterSet "#%<>[\]^`{|}
2.网络访问请求:中文空格字符解码
stringByRemovingPercentEncoding :
Xcode7可能会提示要将stringByAddingPercentEscapesUsingEncoding替换成此方法,要根据是否是解码来区分
//代替stringByAddingPercentEscapesUsingEncoding
let customAllowedSet = NSCharacterSet(charactersInString:"`#%^{}\"[]|\\<> ").invertedSet
NSString *resourcePath = @"http://www.jianshu.com/users/ac47ba96ae2f/latest_articles";
NSString *encodePath ;
if (!IOS7_OR_LATER) {
encodePath = [resourcePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
} else {
encodePath = [resourcePath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
}
举例:
原字符串为:
NSString *url = @"ertehtt""p://xxdsdscrg?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
CFURLCreateStringByAddingPercentEscapes方法为:
CFStringRef encodedCFString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef) url, nil, CFSTR("?!@#$^&%*+,:;='\"`<>()[]{}/\\| "), kCFStringEncodingUTF8);
NSString *encodedString = [[NSString alloc] initWithString:(__bridge_transfer NSString*) encodedCFString];
stringByAddingPercentEncodingWithAllowedCharacters方法为:
NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
NSString *encodedUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
NSLog(@"\n%@\n%@",encodedUrl,encodedString);
之后获得的字符串为一致的。
问题实例:
我有一个有效的 url,我得到 不受支持的 url 错误。正如你可以看到 http://
//http://www.jianshu.com/users/ac47ba96ae2f/latest_articles
Error description=Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x78f97920 {NSUnderlyingError=0x79f78bd0 "unsupported URL", NSLocalizedDescription=unsupported URL}
这是我想试图如何初始化 url:
方法 1:
NSString *path = @"http://www.jianshu.com/users/ac47ba96ae2f/latest_articles";
NSURL *url = [NSURL URLWithString:path];
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];
方法 2:
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.jianshu.com/users/ac47ba96ae2f/latest_articles"]];
解决方法 :
URL 不能包含 ASCII 字符集中, 不是必须这样的字符进行转义的字符。
使用 stringByAddingPercentEncodingWithAllowedCharacters
字符集 URLQueryAllowedCharacterSet
NSString *path = @"http://www.jianshu.com/users/ac47ba96ae2f/latest_articles";
NSString *escapedPath = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSLog(@"escapedPath: %@", escapedPath);
输出:
escapedPath: http://www.jianshu.com/users/ac47ba96ae2f/latest_articles
作者:Kingsleeeey
链接:https://www.jianshu.com/p/dbb15f019127
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具