我正在尝试使用Swift从URL获取参数。我们说我有以下网址:
http://mysite3994.com?test1=blah&test2=blahblah
如何获取test1和test2的值?
答案 0 :(得分:113)
您可以使用以下代码获取参数
func getQueryStringParameter(url: String, param: String) -> String? {
guard let url = URLComponents(string: url) else { return nil }
return url.queryItems?.first(where: { $0.name == param })?.value
}
调用类似let test1 = getQueryStringParameter(url, param: "test1")
其他带扩展名的方法:
extension URL {
public var queryParameters: [String: String]? {
guard
let components = URLComponents(url: self, resolvingAgainstBaseURL: true),
let queryItems = components.queryItems else { return nil }
return queryItems.reduce(into: [String: String]()) { (result, item) in
result[item.name] = item.value
}
}
}
答案 1 :(得分:43)
第1步:创建网址扩展
extension URL {
func valueOf(_ queryParamaterName: String) -> String? {
guard let url = URLComponents(string: self.absoluteString) else { return nil }
return url.queryItems?.first(where: { $0.name == queryParamaterName })?.value
}
}
第2步:如何使用扩展程序
let newURL = URL(string: "http://mysite3994.com?test1=blah&test2=blahblah")!
newURL.valueOf("test1") // Output i.e "blah"
newURL.valueOf("test2") // Output i.e "blahblah"
答案 2 :(得分:17)
我还做了一个URL扩展,但是将查询参数查找放到了下标中。
extension URL {
subscript(queryParam:String) -> String? {
guard let url = URLComponents(string: self.absoluteString) else { return nil }
return url.queryItems?.first(where: { $0.name == queryParam })?.value
}
}
用法:
let url = URL(string: "http://some-website.com/documents/127/?referrer=147&mode=open")!
let referrer = url["referrer"] // "147"
let mode = url["mode"] // "open"
答案 3 :(得分:4)
当链接指向在Angular上创建的网站时,似乎没有现有答案可用。这是因为Angular的路径通常在所有链接中都包含一个#
(哈希)符号,这导致url.queryItems
始终返回nil。
如果链接看起来像这样:http://example.com/path/#/morepath/aaa?test1=blah&test2=blahblah
然后只能从url.fragment
获得参数。在@Matt的扩展中添加了一些附加的解析逻辑之后,更通用的代码将如下所示:
extension URL {
subscript(queryParam: String) -> String? {
guard let url = URLComponents(string: self.absoluteString) else { return nil }
if let parameters = url.queryItems {
return parameters.first(where: { $0.name == queryParam })?.value
} else if let paramPairs = url.fragment?.components(separatedBy: "?").last?.components(separatedBy: "&") {
for pair in paramPairs where pair.contains(queryParam) {
return pair.components(separatedBy: "=").last
}
return nil
} else {
return nil
}
}
}
用法保持不变:
let url = URL(string: "http://example.com/path/#/morepath/aaa?test1=blah&test2=blahblah")!
let referrer = url["test1"] // "blah"
let mode = url["test2"] // "blahblah"
答案 4 :(得分:0)
另一种方法是在URL上创建扩展名以返回组件,然后在[URLQueryItem]上创建扩展名以从queryItems中检索值。
extension URL {
var components: URLComponents? {
return URLComponents(url: self, resolvingAgainstBaseURL: false)
}
}
extension Array where Iterator.Element == URLQueryItem {
subscript(_ key: String) -> String? {
return first(where: { $0.name == key })?.value
}
}
这是如何使用它的一个示例:
if let urlComponents = URL(string: "http://mysite3994.com?test1=blah&test2=blahblah")?.components,
let test1Value = urlComponents.queryItems?["test1"] {
print(test1Value)
}