-1

I am refering this answer to parse url but when parse this url

NSString *urlString = @"https://www.example.com/product-detail?journey_id=123456&iswa=1";

I am getting my 1st key is https://www.example.com/product-detail?journey_id But I need only journey_id as my key.

This is what I have done in coding:

NSString *urlString = @"https://www.example.com/product-detail?journey_id=123456&iswa=1";
        
NSMutableDictionary *waLoginDictionary = [[NSMutableDictionary alloc] init];
NSArray *urlComponents = [urlString componentsSeparatedByString:@"&"];
                            
for (NSString *keyValuePair in urlComponents) {
NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];
NSString *key = [[pairComponents firstObject] stringByRemovingPercentEncoding];
NSString *value = [[pairComponents lastObject] stringByRemovingPercentEncoding];
[waLoginDictionary setObject:value forKey:key];

}
                            
NSLog(@"%@", waLoginDictionary);

I am getting This output:

{
"https://www.example.com/product-detail?journey_id" = 123456;
iswa = 1;
} 
Sarthak
  • 29
  • 1
  • 6
  • 1
    Read all answers on a question. Sometimes there are old, and there have been iOS/SDK/Lib improvement since then, as stated in the first line of the answer (ie: "edit (June 2018): "this answer is better". Apple added `NSURLComponents` in iOS 7"), and since the solution is at author discretion, they don't always select the "best one". – Larme Apr 25 '22 at 06:22

1 Answers1

3

The answer you are referring to is outdated and has been updated accordingly by the writer himself. Apple added [URLQueryItem] in the URLComponent object.

Try this.

Swift

    let urlString = "https://www.example.com/product-detail?journey_id=123456&iswa=1"
    var dict: [String : String] = [:]
    if let urlComponents = URLComponents(string: urlString), let queryItems = urlComponents.queryItems {
        for item in queryItems {
            dict[item.name] = item.value
        }
    }
    print("dict : \(dict)")

Objective - C

NSString *urlString = @"https://www.example.com/product-detail?journey_id=123456&iswa=1";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];

NSURLComponents *urlComponents = [NSURLComponents componentsWithString:urlString];
NSArray *queryItems = [urlComponents queryItems];

for (NSURLQueryItem *item in queryItems) {
    [dict setValue:item.value forKey:item.name];
}

NSLog(@"dict %@", dict);
Prasad Parab
  • 381
  • 1
  • 12
  • Yes this solution is exactly what I want ..it will be very use full if we can check that, If my url contain `iswa == 1` then only we have to add parameters in dict ? and we do not need to add iswa parameter in dict. – Sarthak Apr 25 '22 at 05:48
  • Yes, you can check and add parameters as you wish in the dict while iterating through the query items. – Prasad Parab Apr 25 '22 at 05:55
  • [here](https://stackoverflow.com/a/36705589/18362158) I copied this solution below ViewDidload method but it showing me error : "Use of undeclared identifier 'NSURLDownload'". – Sarthak Apr 26 '22 at 13:44