I need to parse a URL string like this one:
&ad_eurl=http://www.youtube.com/video/4bL4FI1Gz6s&hl=it_IT&iv_logging_level=3&ad_flags=0&ends
- (NSString *)getLoginTokenFromUrl:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
NSArray *queryStrings = [url.query componentsSeparatedByString:@"&"];
NSMutableDictionary *queryParams = [[NSMutableDictionary alloc] init];
for (NSString *qs in queryStrings) {
// Get the parameter name
NSArray *components = [qs componentsSeparatedByString:@"="];
NSString *key = [components objectAtIndex:0];
// Get the parameter value
NSString *value;
if (components.count > 1) {
value = [components objectAtIndex:1];
}
else {
value = @"";
}
value = [value stringByReplacingOccurrencesOfString:@"+" withString:@" "];
value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
queryParams[key] = value;
}
return [queryParams objectForKey:@"login_token"];
}
A late solution to this in the form of a Swift 3 extension on URL
extension URL {
func value(for paramater: String) -> String? {
let queryItems = URLComponents(string: self.absoluteString)?.queryItems
let queryItem = queryItems?.filter({$0.name == paramater}).first
let value = queryItem?.value
return value
}
}
gist
If you're using NSURLComponents
the following concise extension will also do the trick:
extension NSURLComponents {
func getQueryStringParameter(name: String) -> String? {
return (self.queryItems? as [NSURLQueryItem])
.filter({ (item) in item.name == name }).first?
.value()
}
}
For iOS8 and above using NSURLComponents
:
+(NSDictionary<NSString *, NSString *> *)queryParametersFromURL:(NSURL *)url {
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
NSMutableDictionary<NSString *, NSString *> *queryParams = [NSMutableDictionary<NSString *, NSString *> new];
for (NSURLQueryItem *queryItem in [urlComponents queryItems]) {
if (queryItem.value == nil) {
continue;
}
[queryParams setObject:queryItem.value forKey:queryItem.name];
}
return queryParams;
}
For iOS 8 below:
+(NSDictionary<NSString *, NSString *> *)queryParametersFromURL:(NSURL *)url
NSMutableDictionary<NSString *, NSString *> * parameters = [NSMutableDictionary<NSString *, NSString *> new];
[self enumerateKeyValuePairsFromQueryString:url.query completionblock:^(NSString *key, NSString *value) {
parameters[key] = value;
}];
return parameters.copy;
}
- (void)enumerateKeyValuePairsFromQueryString:(NSString *)queryString completionBlock:(void (^) (NSString *key, NSString *value))block {
if (queryString.length == 0) {
return;
}
NSArray *keyValuePairs = [queryString componentsSeparatedByString:@"&"];
for (NSString *pair in keyValuePairs) {
NSRange range = [pair rangeOfString:@"="];
NSString *key = nil;
NSString *value = nil;
if (range.location == NSNotFound) {
key = pair;
value = @"";
}
else {
key = [pair substringToIndex:range.location];
value = [pair substringFromIndex:(range.location + range.length)];
}
key = [self decodedStringFromString:key];
key = key ?: @"";
value = [self decodedStringFromString:value];
value = value ?: @"";
block(key, value);
}
}
+ (NSString *)decodedStringFromString:(NSString *)string {
NSString *input = shouldDecodePlusSymbols ? [string stringByReplacingOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, string.length)] : string;
return [input stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
This code is work in three cases
1.http://www.youtube.com/watch?v=VWsl7C-y7EI&feature=youtu.be
2.http://youtu.be/lOvcFqQyaDY
3.http://www.youtube.com/watch?v=VWsl7C-y7EI
NSArray *arr = [youtubeurl componentsSeparatedByString:@"v="];
NSString *youtubeID;
if([arr count]>0)
{
if([arr count]==1){
youtubeID= [[youtubeurl componentsSeparatedByString:@"/"] lastObject];
}
else{
NSArray *urlComponents = [[arr objectAtIndex:1] componentsSeparatedByString:@"&"];
youtubeID=[urlComponents objectAtIndex:0];
}
}
If you want to do the same thing in swift, you can use an extension.
extension NSURL {
func queryDictionary() -> [String:String] {
let components = self.query?.componentsSeparatedByString("&")
var dictionary = [String:String]()
for pairs in components ?? [] {
let pair = pairs.componentsSeparatedByString("=")
if pair.count == 2 {
dictionary[pair[0]] = pair[1]
}
}
return dictionary
}
}