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

How to get the value of url parameter using swift 4.1

我正在尝试获取 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: { [=13=].name == queryParameterName})?.value 
    } 
}
    
    let newURL = URL(string: "assetlinkasia.no-ip.biz:8001/hf_tracker/api/…)! 
    newURL.valueOf("toDate") 
    newURL.valueOf("fromDate") 

如何只获取日期不获取时间?

这是你可以做到的方式,

您的 URL 分机,来自

extension URL {
    func valueOf(_ queryParamaterName: String) -> String? {
        guard let url = URLComponents(string: self.absoluteString) else { return nil }
        return url.queryItems?.first(where: { [=10=].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

在我的应用程序中,使用一个辅助函数来解析 url 和 returns 一个可选的字典:

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

    // Create all parameters dictionary
    let queryArray = urlQuery.split { [=10=] == "&" }.map(String.init)
    var parametersDict: [String: String] = [:]
    for queryParameter in queryArray {
        // split the queryParam into key / value
        let keyValueArray = queryParameter.split{ [=10=] == "=" }.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 { [=12=] == "&" }.map(String.init)
        var parametersDict: [String: String] = [:]
        for queryParameter in queryArray {
            // split the queryParam into key / value
            let keyValueArray = queryParameter.split{ [=12=] == "=" }.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)
}