如何从URLscheme中获取参数。
我在我的iPhone应用程序中使用URLscheme,从切换用户到Safari浏览器的页面,以及从Web页面点击一个button,我将恢复到应用程序当时,一些参数通过网页传递
myapp://parameter=1
我如何从我的应用程序中find这个参数? 我已经放了一个断点
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)ur
但是,当回复,它不被调用。
你可以使用这个url host
来查找参数,其中参数可以是任何types, http://
或custom tag custom://
之后的任何内容都将被捕获到下面的代码中
NSString *urlParameter = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
如果你有多个参数,那么你可以使用componentsSeparatedByString
方法来分离参数
NSString *query = [url query]; NSArray *queryComponents = [query componentsSeparatedByString:@"&"];
现在你在queryComponents中得到了所有的string
param1=value
现在你可以得到这样的第一个价值
NSArray *firstParam = [[queryComponents objectAtIndex:0] componentsSeparatedByString:@"="]; [firstParam objectAtIndex:0] is the name of param [firstParam objectAtIndex:1] is value of the param
试试这个代码。 即使你在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]]; }