在 Swift 中拆分 URL 查询

问题描述 投票:0回答:3

我有以下 URL 查询:

encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN

如何拆分查询以获得

encodedMessage
signature
值?

swift url
3个回答
16
投票

实现此目的的正确方法是使用 URLComponents:

基于 RFC 3986 设计用于解析 URL 并构造 来自其组成部分的 URL。

通过获取url组件host字符串和queryItems数组,如下:

if let urlComponents = URLComponents(string: "http://mydummysite.com?encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN"), let host = urlComponents.host, let queryItems = urlComponents.queryItems {

    print(host) // mydummysite.com

    print(queryItems) // [encodedMessage=PD94bWwgdmVyNlPg==, signature=kcig33sdAOAr/YYGf5r4HGN]
}

queryItems
数组包含URLQueryItem对象,该对象具有
name
value
属性:

if let urlComponents = URLComponents(string: "http://mydummysite.com?encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN"),let queryItems = urlComponents.queryItems {

    // for example, we will get the first item name and value:
    let name = queryItems[0].name // encodedMessage
    let value = queryItems[0].value // PD94bWwgdmVyNlPg==
}

还有:

如果您收到的查询没有完整的 url,我建议您使用一个非常简单的技巧,通过添加一个虚拟主机作为查询字符串的前缀,如下所示:

let myQuery = "encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN"
let myDummyUrlString = "http://stackoverflow.com?" + myQuery

if let urlComponents = URLComponents(string: myDummyUrlString),let queryItems = urlComponents.queryItems {
    // for example, we will get the first item name and value:
    let name = queryItems[0].name // encodedMessage
    let value = queryItems[0].value // PD94bWwgdmVyNlPg==
} else {
    print("invalid url")
}

5
投票

您可以通过这种方式获取键值对:

let str = "encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN"
let arr = str.components(separatedBy:"&")
var data = [String:Any]()
for row in arr {
    let pairs = row.components(separatedBy:"=")
    data[pairs[0]] = pairs[1]
}
let message = data["encodedMessage"]
let sig = data["signature"]

我不确定这是否是您正在寻找的。如果不是,您能进一步澄清一下您在寻找什么吗?


0
投票

这是我用来用

swift5

来破坏 URL 的方法
func breakURL(_ urlString: String) -> URLComponents? {
    // Create a URL from the given urlString
    guard let url = URL(string: urlString) else {
        print("Invalid URL")
        return nil
    }
    
    // Get the components of the URL
    if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
        return urlComponents
    } else {
        print("Failed to parse URL components")
        return nil
    }
}

以下是使用方法。

if let urlComponents = breakURL("https://www.example.com/path/to/resource?param1=value1&param2=value2") {
    print("Scheme: \(urlComponents.scheme ?? "N/A")") // https
    print("Host: \(urlComponents.host ?? "N/A")") //www.example.com
    print("Path: \(urlComponents.path)") //path/to/resource
    print("Query: \(urlComponents.query ?? "N/A")") //param1=value1&param2=value2
}

如果您想中断路径或查询,您可以使用索引号获取特定值,或者只是在其上放置循环...

for queryItem in urlComponents.queryItems ?? [] {
    print("\(queryItem.name): \(queryItem.value ?? "N/A")") 
}
© www.soinside.com 2019 - 2024. All rights reserved.