Fetch parameters from URL
In Swift, Many times you need to fetch parameters from URL. Like you are using a web view where every time you get a URL in return and then you have to fetch particular parameter for match result you have to traverse complete URL and it also needs to fetch Parameter from URL when you are using Universal linking it’s tough to fetch by using string match functions so find an easy way. Let’s take an example:-
You have a URL for an ex:- www.xyz.com?name=hello&id=1&status=true
So you want to fetch the value of id parameter. By using this function you fetch you parameter.
1 2 3 4 |
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 } |
How to fetch:-
1 |
let param = getQueryStringParameter(url, param: "id") |
Thanks for go through this blog. Stay cool and stay updated.