ios 获取 URL 参数的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41421686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the value of URL Parameters
提问by user2423476
I am trying to get the parameters from a URL using Swift. Let's say I have the following URL:
我正在尝试使用 Swift 从 URL 获取参数。假设我有以下网址:
http://mysite3994.com?test1=blah&test2=blahblah
How can I get the values of test1 and test2?
如何获得 test1 和 test2 的值?
回答by Parth Adroja
You can use the belowCode to get the param
您可以使用下面的代码来获取参数
func getQueryStringParameter(url: String, param: String) -> String? {
guard let url = URLComponents(string: url) else { return nil }
return url.queryItems?.first(where: { 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
}
}
}
.name == param })?.value
}
Call the method like let test1 = getQueryStringParameter(url, param: "test1")
像这样调用方法 let test1 = getQueryStringParameter(url, param: "test1")
Other method with extension:
其他扩展方法:
extension URL {
func valueOf(_ queryParamaterName: String) -> String? {
guard let url = URLComponents(string: self.absoluteString) else { return nil }
return url.queryItems?.first(where: { 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"
.name == queryParamaterName })?.value
}
}
回答by Bhuvan Bhatt
Step 1:Create URL extension
第 1 步:创建 URL 扩展
extension URL {
subscript(queryParam:String) -> String? {
guard let url = URLComponents(string: self.absoluteString) else { return nil }
return url.queryItems?.first(where: { let url = URL(string: "http://some-website.com/documents/127/?referrer=147&mode=open")!
let referrer = url["referrer"] // "147"
let mode = url["mode"] // "open"
.name == queryParam })?.value
}
}
Step 2:How to use the extension
第 2 步:如何使用扩展
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: { let url = URL(string: "http://example.com/path/#/morepath/aaa?test1=blah&test2=blahblah")!
let referrer = url["test1"] // "blah"
let mode = url["test2"] // "blahblah"
.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
}
}
}
回答by Matt Long
I also made a URL extension, but put the query param lookup into a subscript.
我也做了一个 URL 扩展,但是把查询参数查找放到了一个下标中。
extension URL {
var components: URLComponents? {
return URLComponents(url: self, resolvingAgainstBaseURL: false)
}
}
extension Array where Iterator.Element == URLQueryItem {
subscript(_ key: String) -> String? {
return first(where: { if let urlComponents = URL(string: "http://mysite3994.com?test1=blah&test2=blahblah")?.components,
let test1Value = urlComponents.queryItems?["test1"] {
print(test1Value)
}
.name == key })?.value
}
}
Usage:
用法:
##代码##回答by Vitalii
It appears that none of existing answers work when the link leads to a web site created on Angular. This is because Angular's paths often include a #
(hash) symbol in all links, which results in url.queryItems
always returning nil.
当链接指向在 Angular 上创建的网站时,似乎没有现有的答案有效。这是因为 Angular 的路径通常#
在所有链接中都包含一个(哈希)符号,这导致url.queryItems
始终返回 nil。
If a link looks like this: http://example.com/path/#/morepath/aaa?test1=blah&test2=blahblah
如果链接如下所示: http://example.com/path/#/morepath/aaa?test1=blah&test2=blahblah
Then the parameters can only be obtained from url.fragment
. With some additional parsing logic added to @Matt's extension, a more universal code would look like this:
那么参数只能从 中获得url.fragment
。在@Matt 的扩展中添加了一些额外的解析逻辑,一个更通用的代码看起来像这样:
Usage remains same:
用法保持不变:
##代码##回答by totiG
Another way of doing this is to create an extension on URL to return the components, and then create an extension on [URLQueryItem] to retrieve the value from the queryItems.
另一种方法是在 URL 上创建扩展以返回组件,然后在 [URLQueryItem] 上创建扩展以从 queryItems 中检索值。
##代码##And this is an example of how this could be used:
这是如何使用它的示例:
##代码##