Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to split and convert url params into string values

I would like to split a custom url for app opening in iPhone into values, my scheme would be something like:

appname://user=jonsmith&message=blah%20blah

Where I would like to be able to get "user" and "message" as two NSStrings. Any advice on best approach?

like image 504
mootymoots Avatar asked Jan 22 '10 18:01

mootymoots


2 Answers

Assuming your url is in an NSURL object called url:

NSMutableDictionary *queryParams = [[NSMutableDictionary alloc] init];
NSArray *components = [[url query] componentsSeparatedByString:@"&"];

for (NSString *component in components) {
    NSArray *pair = [component componentsSeparatedByString:@"="];

    [queryParams setObject:[[pair objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding: NSMacOSRomanStringEncoding]
                    forKey:[pair objectAtIndex:0]]; 
}

...

[queryParams release];
like image 51
Frank Schmitt Avatar answered Oct 25 '22 05:10

Frank Schmitt


Use Google's gtm_dictionaryWithHttpArgumentsString NSDictionary category

http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMNSDictionary%2BURLArguments.h

like image 31
Ford Avatar answered Oct 25 '22 04:10

Ford