112

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
  • 51,886
  • 29
  • 155
  • 197
user2423476
  • 1,975
  • 8
  • 27
  • 38

5 Answers5

242

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: { $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
        }
    }
}
LinusGeffarth
  • 24,641
  • 28
  • 109
  • 162
Parth Adroja
  • 12,447
  • 5
  • 37
  • 70
96

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,063
  • 1
  • 28
  • 38
Bhuvan Bhatt
  • 2,896
  • 1
  • 17
  • 19
32

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,295
  • 4
  • 72
  • 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
11

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
  • 3,939
  • 1
  • 34
  • 44
10

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,069
  • 3
  • 29
  • 30