How to take the parameter from URL scheme.

18,194

Solution 1

You can use this url host to find out the parameters, where parameters could be of any kind, anything after http:// or custom tag custom:// will be caught in the following code

 NSString *urlParameter = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

If you have multiple parameters then you could use componentsSeparatedByString method to separate the parameters

Solution 2

NSString *query = [url query];
NSArray *queryComponents = [query componentsSeparatedByString:@"&"];

Now you got all strings in queryComponents like

param1=value

Now you can get first value like that

NSArray *firstParam = [[queryComponents objectAtIndex:0] componentsSeparatedByString:@"="];

[firstParam objectAtIndex:0] is the name of param
[firstParam objectAtIndex:1] is value of the param

Solution 3

Try this code. this ll useful even if you had numbers of parameters and values in url.

NSURL *url = [NSURL URLWithString:@"xyz.com/result.php?data1=1123660801&data2=250072028055&presult=SUCCESS"];

NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
NSArray *queryItems = [components queryItems];

NSMutableDictionary *dict = [NSMutableDictionary new];

for (NSURLQueryItem *item in queryItems)
{
    [dict setObject:[item value] forKey:[item name]];
}
Share:
18,194
NITHIN SHAHRUKH
Author by

NITHIN SHAHRUKH

Updated on June 27, 2022

Comments

  • NITHIN SHAHRUKH
    NITHIN SHAHRUKH almost 2 years

    I am using URL scheme in my iPhone app,from a page I switch user to safari,and a button click from Web page,I am reverting back to app At that time ,some parameters are passed by the web page like

     myapp://parameter=1
    

    How can I find this parameter from my app ? I have placed a break point in

        -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)ur
    

    but while revert back.it is not get called.

  • nsgulliver
    nsgulliver about 11 years
    @Mert I have tried query for my custom links when I created, I never got anything inside query, might be because I had custom parameters after customtag:// so i preferred using [url host]
  • nsgulliver
    nsgulliver about 11 years
    just use in this -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url method, just try to NSLog the value there when you click the custom link, it will print out parameter=1
  • Mert
    Mert about 11 years
    If you have a URL like that customschema://myhost/?param=1&secondParam=2 it works. It is common, the urls look like this. I use myhost for different actions in my app like showAlert, showsomeview etc... Then I can check what to do
  • Mert
    Mert about 11 years
    You can use it in your -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url method. You have the url here
  • 2am
    2am about 10 years
    Do you have any idea how this can be achieved in vanilla c++? Is there an equivalent function in Carbon?
  • Ahmad Al-Attal
    Ahmad Al-Attal over 6 years
    this should be the answer, just make it more specific to answer the question.