• iOS url出现特殊字符处理 -- stringByAddingPercentEncodingWithAllowedCharacters


    stringByAddingPercentEscapesUsingEncoding(只对 `#%^{}[]|"<> 加空格共14个字符编码,不包括”&?”等符号), ios9将淘汰,建议用 stringByAddingPercentEncodingWithAllowedCharacters 方法

    stringByAddingPercentEncodingWithAllowedCharacters  需要传一个  NSCharacterSet  对象(关于  NSCharacterSet 这篇 文章说的很好)

    如[NSCharacterSet  URLQueryAllowedCharacterSet]

    URLFragmentAllowedCharacterSet  "#%<>[]^`{|}
    
    URLHostAllowedCharacterSet      "#%/<>?@^`{|}
    
    URLPasswordAllowedCharacterSet  "#%/:<>?@[]^`{|}
    
    URLPathAllowedCharacterSet      "#%;<>?[]^`{|}
    
    URLQueryAllowedCharacterSet    "#%<>[]^`{|}
    
    URLUserAllowedCharacterSet      "#%/:<>?@[]^`

    (2)网络访问请求:中文空格字符解码

    stringByRemovingPercentEncoding  ----  xcode7可能会提示要将stringByAddingPercentEscapesUsingEncoding替换成此方法,要根据是否是解码来区分

    代替  stringByAddingPercentEscapesUsingEncoding

    let customAllowedSet =  NSCharacterSet(charactersInString:"`#%^{}"[]|\<> ").invertedSet
    
    NSString *urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"?!@#$^&%*+,:;='"`<>()[]{}/\| "] invertedSet]];
  • 相关阅读:
    win7与centos虚拟机的共享文件夹创建
    MySQL视图
    MySQL分区表与合并表
    PHP读写XML文件的四种方法
    备份与恢复
    MySQL日志
    MySQL锁问题
    优化数据库对象
    ActiveReport资料
    对ArrayList 进行深拷贝
  • 原文地址:https://www.cnblogs.com/jiuyi/p/10113578.html
Copyright © 2020-2023  润新知