只需将片段属性作为查询字符串传递给新的 URLComponent 并读取解析的查询对象:
let url = URL(string: "https://example.com/auth-callback#access_token=mytoken&expires_in=36000&scope=zap+profile")
var components = URLComponents()
components.query = url.fragment
for item in components.queryItems! {
print("\(item.name): \(item.value)")
}
Run Code Online (Sandbox Code Playgroud)