-1

For example, I got something format like this:

link://thisIsAction?param1=value1&param2=value2

and it may have one param like this

link://thisIsAction?param1=value1

or don't have any param:

link://thisIsAction

How can I extract the thisIsAction, and the param, and value? The param may not start with the prefix param, it may just simple as p.

And I want the param and value return in a dict... How can I do so? Thanks.

DNB5brims
  • 29,344
  • 50
  • 131
  • 195
  • I tried using subString... but seems no luck. – DNB5brims May 23 '12 at 08:54
  • What do you mean by "no luck"? I'd say "please post your code and explain what is going wrong", but better answers than fixing it have already been given, but please remember this for future questions. – JeremyP May 23 '12 at 10:05

2 Answers2

1

Convert the string to a NSURL and then use the API to extract different parts of it, including the query string parameters.

You can get the host using

NSURL *url = [NSURL URLWithString:yourUrlString]; 
NSString *scheme = [url scheme]; // gives "link" 
NSString *host = [url host];  // gives thisIsAction in your example
NSString *query = [url query]; // gives "param=value1&param=value2"

Documentation on it here

As pointed out by @Rene, to get the query value in a dictionary, follow the top answer on Parse NSURL query parameter

Community
  • 1
  • 1
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
1

there's another question, where you could find your answer if you convert it to NSURL first. Parse NSURL query property

Community
  • 1
  • 1
Herm
  • 2,956
  • 19
  • 32