• 关于 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

    https://www.jianshu.com/p/dbb15f019127?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

    第一:

    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
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    IntentService使用以及源码分析
    Android HandlerThread源码解析
    Android Handler消息机制源码解析
    Gradle技术之四
    Android EditText实现小数点后几位的终级方案
    Gradle系列之三 Gradle概述以及生命周期
    Gradle系列之二 Groovy对文件的操作
    Gradle系列之一 Groovy语法精讲
    Context源码分析
    用EXCLE群发outlook邮件
  • 原文地址:https://www.cnblogs.com/itlover2013/p/15563457.html
Copyright © 2020-2023  润新知