如何使用swift 4.1获取url参数的值

Posted

技术标签:

【中文标题】如何使用swift 4.1获取url参数的值【英文标题】:How to get the value of url parameter using swift 4.1 【发布时间】:2018-05-30 07:09:37 【问题描述】:

我正在尝试获取 URL 的参数,并且我尝试仅获取 URL 的日期而不是时间。

http://aa.no-ip.biz:8001/hf_tracker/api/history.php?accesskey=12345&Vehilce=1618&FromDate=2018-05-10 13:11&ToDate=2018-05-14 12:11

代码:

extension URL 
    func valueOf(_ queryParameterName: String) -> String?  
        guard let url = URLComponents(string: self.absoluteString) else  
              return nil 
         

        return url.queryItems?.first(where:  $0.name == queryParameterName)?.value 
     

    
    let newURL = URL(string: "assetlinkasia.no-ip.biz:8001/hf_tracker/api/…)! 
    newURL.valueOf("toDate") 
    newURL.valueOf("fromDate") 

我怎样才能只得到日期而不是时间?

【问题讨论】:

向我们展示你的代码,你尝试了什么 贴出相关代码方便理解@Rehan Meo 扩展 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 let newURL = URL(string: " assetlinkasia.no-ip.biz:8001/hf_tracker/api/…)!newURL.valueOf("toDate") newURL.valueOf("fromDate") 虽然问题是三年前的问题,但即使在今天,检查URLComponents(string: self.absoluteString) 也毫无意义,永远不会失败。 【参考方案1】:

你可以这样做,

您的网址扩展名,来自here

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
    

你的代码放在这里,

let string = "http://aa.no-ip.biz:8001/hf_tracker/api/history.php?accesskey=12345&Vehilce=1618&FromDate=2018-05-10 13:11&ToDate=2018-05-14 12:11"
let test = string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let url = URL(string: test!)!
let fromDate = url.valueOf("FromDate")
let toDate = url.valueOf("ToDate")

let date1 = fromDate?.components(separatedBy: " ").first
print(date1)

let date2 = toDate?.components(separatedBy: " ").first
print(date2 )

输出将在下方,

2018-05-10
2018-05-14

【讨论】:

【参考方案2】:

在我的应用程序中,使用解析 url 并返回可选字典的辅助函数:

func parameters(for url: URL) -> [String: String]? 
    guard let urlQuery = url.query else  return nil 

    // Create all parameters dictionary
    let queryArray = urlQuery.split  $0 == "&" .map(String.init)
    var parametersDict: [String: String] = [:]
    for queryParameter in queryArray 
        // split the queryParam into key / value
        let keyValueArray = queryParameter.split $0 == "=" .map(String.init)
        let key = keyValueArray.first!
        let value = keyValueArray.last!.removingPercentEncoding!
        parametersDict.updateValue(value, forKey: key)
    

    return parametersDict

并像这样使用它:

let params = parameters(for: URL(string: "https://someurl.com?name=john")!)
if let name = params?["name"] 
    print(name)

或者您可以将 var 添加到 URL 扩展:

extension URL 
    var parameters: [String: String]? 
        guard let urlQuery = self.query else  return nil 

        // Create all parameters dictionary
        let queryArray = urlQuery.split  $0 == "&" .map(String.init)
        var parametersDict: [String: String] = [:]
        for queryParameter in queryArray 
            // split the queryParam into key / value
            let keyValueArray = queryParameter.split $0 == "=" .map(String.init)
            let key = keyValueArray.first!
            let value = keyValueArray.last!.removingPercentEncoding!
            parametersDict.updateValue(value, forKey: key)
        

        return parametersDict
    

并获取参数:

let params = URL(string: "https://someurl.com?name=john")!.parameters
if let name = params?["name"] 
    print(name)

【讨论】:

以上是关于如何使用swift 4.1获取url参数的值的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Swift 3 xcode8 的 UIWebView 中的 url 获取查询字符串参数?

js获取url中指定参数的值(兼容hash)

Swift 中的 iOS 共享扩展抓取 URL

abap如何获取url后面参数

JS获取URL参数

如何在页面获取页面控件的值,并作为参数传递