Created
February 1, 2018 16:59
-
-
Save andr3a88/7b5faacb107002454ac2bd516e6b9c65 to your computer and use it in GitHub Desktop.
URL extension to pick the parameter from url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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