How to take the parameter from URL scheme.

白昼怎懂夜的黑 提交于 2019-12-20 17:29:21

问题


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.


回答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




回答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



回答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]];
}


来源:https://stackoverflow.com/questions/15360478/how-to-take-the-parameter-from-url-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!