I have the following URL query:
encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN
How can I split the query to get the of encodedMessage and signature values?
I have the following URL query:
encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN
How can I split the query to get the of encodedMessage and signature values?
The right way to achieve this is to work with URLComponents:
A structure designed to parse URLs based on RFC 3986 and to construct URLs from their constituent parts.
By getting the url components host string and queryItems array, as follows:
if let urlComponents = URLComponents(string: "http://mydummysite.com?encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN"), let host = urlComponents.host, let queryItems = urlComponents.queryItems {
print(host) // mydummysite.com
print(queryItems) // [encodedMessage=PD94bWwgdmVyNlPg==, signature=kcig33sdAOAr/YYGf5r4HGN]
}
queryItems array contains URLQueryItem objects, which have name and value properties:
if let urlComponents = URLComponents(string: "http://mydummysite.com?encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN"),let queryItems = urlComponents.queryItems {
// for example, we will get the first item name and value:
let name = queryItems[0].name // encodedMessage
let value = queryItems[0].value // PD94bWwgdmVyNlPg==
}
Also:
In case of you are getting the query without the full url, I'd suggest to do a pretty simple trick, by adding a dummy host as a prefix to your query string, as follows:
let myQuery = "encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN"
let myDummyUrlString = "http://stackoverflow.com?" + myQuery
if let urlComponents = URLComponents(string: myDummyUrlString),let queryItems = urlComponents.queryItems {
// for example, we will get the first item name and value:
let name = queryItems[0].name // encodedMessage
let value = queryItems[0].value // PD94bWwgdmVyNlPg==
} else {
print("invalid url")
}
You can get the key value pairs this way:
let str = "encodedMessage=PD94bWwgdmVyNlPg%3D%3D&signature=kcig33sdAOAr%2FYYGf5r4HGN"
let arr = str.components(separatedBy:"&")
var data = [String:Any]()
for row in arr {
let pairs = row.components(separatedBy:"=")
data[pairs[0]] = pairs[1]
}
let message = data["encodedMessage"]
let sig = data["signature"]
I am not sure if that's what you were looking for or not. If it is not, could you please clarify a bit further as to what you are looking for?
This is the method I am using to break URLs with swift5
func breakURL(_ urlString: String) -> URLComponents? {
// Create a URL from the given urlString
guard let url = URL(string: urlString) else {
print("Invalid URL")
return nil
}
// Get the components of the URL
if let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
return urlComponents
} else {
print("Failed to parse URL components")
return nil
}
}
Following is the way to use it.
if let urlComponents = breakURL("https://www.example.com/path/to/resource?param1=value1¶m2=value2") {
print("Scheme: \(urlComponents.scheme ?? "N/A")") // https
print("Host: \(urlComponents.host ?? "N/A")") //www.example.com
print("Path: \(urlComponents.path)") //path/to/resource
print("Query: \(urlComponents.query ?? "N/A")") //param1=value1¶m2=value2
}
if you want to break path or query you can get specific value with index number or just put loop on it ...
for queryItem in urlComponents.queryItems ?? [] {
print("\(queryItem.name): \(queryItem.value ?? "N/A")")
}