Best way to parse URL string to get values for keys?
I also answered this at https://stackoverflow.com/a/26406478/215748.
You can use queryItems
in URLComponents
.
When you get this property’s value, the NSURLComponents class parses the query string and returns an array of NSURLQueryItem objects, each of which represents a single key-value pair, in the order in which they appear in the original query string.
let url = "http://example.com?param1=value1¶m2=param2"let queryItems = URLComponents(string: url)?.queryItemslet param1 = queryItems?.filter({$0.name == "param1"}).firstprint(param1?.value)
Alternatively, you can add an extension on URL to make things easier.
extension URL { var queryParameters: QueryParameters { return QueryParameters(url: self) }}class QueryParameters { let queryItems: [URLQueryItem] init(url: URL?) { queryItems = URLComponents(string: url?.absoluteString ?? "")?.queryItems ?? [] print(queryItems) } subscript(name: String) -> String? { return queryItems.first(where: { $0.name == name })?.value }}
You can then access the parameter by its name.
let url = URL(string: "http://example.com?param1=value1¶m2=param2")!print(url.queryParameters["param1"])
edit (June 2018): this answer is better. Apple added NSURLComponents
in iOS 7.
I would create a dictionary, get an array of the key/value pairs with
NSMutableDictionary *queryStringDictionary = [[NSMutableDictionary alloc] init];NSArray *urlComponents = [urlString componentsSeparatedByString:@"&"];
Then populate the dictionary :
for (NSString *keyValuePair in urlComponents){ NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="]; NSString *key = [[pairComponents firstObject] stringByRemovingPercentEncoding]; NSString *value = [[pairComponents lastObject] stringByRemovingPercentEncoding]; [queryStringDictionary setObject:value forKey:key];}
You can then query with
[queryStringDictionary objectForKey:@"ad_eurl"];
This is untested, and you should probably do some more error tests.
I'm a bit late, but the answers provided until now didn't work as I needed to. You can use this code snippet:
NSMutableDictionary *queryStrings = [[NSMutableDictionary alloc] init];for (NSString *qs in [url.query componentsSeparatedByString:@"&"]) { // Get the parameter name NSString *key = [[qs componentsSeparatedByString:@"="] objectAtIndex:0]; // Get the parameter value NSString *value = [[qs componentsSeparatedByString:@"="] objectAtIndex:1]; value = [value stringByReplacingOccurrencesOfString:@"+" withString:@" "]; value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; queryStrings[key] = value;}
Where url
is the URL you want to parse. You have all of the query strings, escaped, in the queryStrings
mutable dictionary.
EDIT: Swift version:
var queryStrings = [String: String]()if let query = url.query { for qs in query.componentsSeparatedByString("&") { // Get the parameter name let key = qs.componentsSeparatedByString("=")[0] // Get the parameter value var value = qs.componentsSeparatedByString("=")[1] value = value.stringByReplacingOccurrencesOfString("+", withString: " ") value = value.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! queryStrings[key] = value }}