安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析:

url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理

如下:

Swift 复制代码
   guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
      return
   }

后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问。

这表名.urlQueryAllowed并没有包含字符"#"。

也有提供API可以测试:

Swift 复制代码
.urlQueryAllowed.contains("#")

解决方案:

显而易见,只要将字符"#"加到.urlQueryAllowed即可解决这个问题。

CharacterSet提供了三个方法:

Swift 复制代码
取并集
/// Returns a union of the `CharacterSet` with another `CharacterSet`.
public func union(_ other: CharacterSet) -> CharacterSet

取交集
/// Returns an intersection of the `CharacterSet` with another `CharacterSet`.
public func intersection(_ other: CharacterSet) -> CharacterSet

取补集
/// Returns a `CharacterSet` created by removing elements in `other` from `self`.
public func subtracting(_ other: CharacterSet) -> CharacterSet

为CharacterSet增加一个新属性

Swift 复制代码
extension CharacterSet {
    static let allowedCharacters = urlQueryAllowed.union(.init(charactersIn: "#"))
}

上面的url编码方法改为

Swift 复制代码
   guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .allowedCharacters) else {
      return
   }

在编码过程中不对字符#操作即可解决。

寻根究底

.urlQueryAllowed中到底包含哪些字符?

以下是遍历NSCharacterSet字符集一种的实现方式。(摘自:URL详解以及iOS中URLencode和URLdecode

Swift 复制代码
NSString* characters(NSCharacterSet *set){
    NSMutableString *string = [NSMutableString string];
    for (UInt8 plane = 0; plane < 17; plane++) {
        if ([set hasMemberInPlane:plane]){
            UInt32 p0 = (UInt32)plane * 65535;
            UInt32 p1 = (UInt32)(plane + 1) * 65535;
            for (UInt32 i = p0; i < p1; i ++) {
                if([set longCharacterIsMember:i]){
                    [string appendFormat:@"%c",I];
                }
            }
        }
    }
    return string;
 }

原理:OC中的字符是Unicode字符集(Swift也是)Unicode共有17个扇区,每个扇区能表示65535个Unicode字符,用4个字节就可以表示任意Unicode码点,通过遍历Unicode字符集就可以找出NSCharacterSet包含的集合了;

hasMemberInPlane:判断当前字符是否在当前扇区。

longCharacterIsMember:当前字符在字符集中是否存在。

NSCharacterSet提供了标准字符集:部分字符集遍历结果如下:

URLUserAllowedCharacterSet

复制代码
!$&'()*+,-.0123456789;=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~

URLHostAllowedCharacterSet

复制代码
!$&'()*+,-.0123456789:;=ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz~

URLPathAllowedCharacterSet

复制代码
!$&'()*+,-./0123456789:=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~

URLQueryAllowedCharacterSet

复制代码
!$&'()*+,-./0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~

URLPasswordAllowedCharacterSet

复制代码
!$&'()*+,-.0123456789;=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~

URLFragmentAllowedCharacterSet

复制代码
!$&'()*+,-./0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~
相关推荐
恋猫de小郭18 小时前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios
点金石游戏出海1 天前
每周资讯 | Krafton斥资750亿日元收购日本动画公司ADK;《崩坏:星穹铁道》新版本首日登顶iOS畅销榜
游戏·ios·业界资讯·apple·崩坏星穹铁道
旷世奇才李先生1 天前
Swift 安装使用教程
开发语言·ios·swift
90后的晨仔1 天前
Xcode16报错: SDK does not contain 'libarclite' at the path '/Applicati
ios
finger244801 天前
谈一谈iOS线程管理
ios·objective-c
Digitally1 天前
如何将大型视频文件从 iPhone 传输到 PC
ios·iphone
梅名智1 天前
IOS 蓝牙连接
macos·ios·cocoa
美狐美颜sdk1 天前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭2 天前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
泓博2 天前
Objective-c把字符解析成字典
开发语言·ios·objective-c