Skip to content

Instantly share code, notes, and snippets.

@andr3a88
Created February 1, 2018 16:59
Show Gist options
  • Save andr3a88/7b5faacb107002454ac2bd516e6b9c65 to your computer and use it in GitHub Desktop.
Save andr3a88/7b5faacb107002454ac2bd516e6b9c65 to your computer and use it in GitHub Desktop.
URL extension to pick the parameter from url
import UIKit
extension URL {
func getValueForQueryParameter(name: String) -> String? {
let urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true)
return urlComponents?.queryItems?.first(where: { (item) -> Bool in
item.name == name
})?.value
}
}
var urlWithParams = "https://www.google.it/?code=ZjNkYTlkYjYtYjA3OS00NWNmLWE0NDktNjM4NmEyYmU2ODFh8xcVhvZFl3lGkNrtVCopnnENm97d2ltB2x6UBXGcs789vCNaB2EpJKX--xK5fAB85UhovNdfejkMjxnES_C8fg&session_state=455ff0b2f94799d321e1b0ac1a814ea6fc8fc15ae5fc1d231ab0789c24304611.2325193e2e1f3343"
let url = URL(string: urlWithParams)!
let codeValue = url.getValueForQueryParameter(name: "code")
let sessionStateValue = url.getValueForQueryParameter(name: "session_state")
print("code \(codeValue!)")
print("session_state \(sessionStateValue!)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment