122

I am trying to get the parameters from a URL using Swift. Let's say I have the following URL:

http://mysite3994.com?test1=blah&test2=blahblah

How can I get the values of test1 and test2?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
user2423476
  • 2,115
  • 8
  • 28
  • 40

5 Answers5

265

You can use the below code 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: { $0.name == param })?.value
}

Call the method like let test1 = getQueryStringParameter(url, param: "test1")

Other method with extension:

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
        }
    }
}
Will
  • 4,942
  • 2
  • 22
  • 47
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
107

Step 1: Create URL extension

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
    }
}

Step 2: How to use the extension

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"
Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
Bhuvan Bhatt
  • 3,276
  • 2
  • 18
  • 23
37

I also made a URL extension, but put the query param lookup into a subscript.

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
    }
}

Usage:

let url = URL(string: "http://some-website.com/documents/127/?referrer=147&mode=open")!

let referrer = url["referrer"]  // "147"
let mode     = url["mode"]      // "open"
Matt Long
  • 24,438
  • 4
  • 73
  • 99
  • You can use like this, let urlString = URL(string: url.absoluteString)! guard let userName = urlString.valueOf("userName"), let code = urlString.valueOf("code"), let userId = urlString.valueOf("userName") else { return false } print("Details: \(urlString)") print("userName: \(userName)") print("code: \(code)") print("userId: \(userId)") – Mehul Nov 11 '19 at 10:22
  • @Mehul why would you create an URL from another URL `let urlString = URL(string: url.absoluteString)!` is pointless. Btw `URL` has no `valueOf` method. – Leo Dabus Oct 12 '20 at 16:46
  • This should return an Array unless you are completely sure that each Query Item has a unique name. – Alessandro Martin Apr 20 '21 at 08:52
12

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.

If a link looks like this: 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:

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
        }
    }
}

Usage remains same:

let url = URL(string: "http://example.com/path/#/morepath/aaa?test1=blah&test2=blahblah")!

let referrer = url["test1"]  // "blah"
let mode     = url["test2"]  // "blahblah"
Vitalii
  • 4,267
  • 1
  • 40
  • 45
11

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.

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
    }
}

And this is an example of how this could be used:

if let urlComponents = URL(string: "http://mysite3994.com?test1=blah&test2=blahblah")?.components,
    let test1Value = urlComponents.queryItems?["test1"] {
    print(test1Value)
}
totiDev
  • 5,189
  • 3
  • 30
  • 30