开发IOS项目的时候,有可能会遇到两个APP应用相互调用的需求,比如说:支付宝支付......等等。
下面来详细介绍实现的步骤:
1,添加URL Types项
a,打开项目中info.plist文件,在infomation property list项下面增加一项URL Typs
2,配置URL Scheme
a,展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme
b,展开URL Scheme,将Item1的内容修改为myapp
(其他应用可通过”myapp://“来访问此自定义URL的应用程序)
3,其他应用的跳转
作为调用者的我,需要通过:
- NSString *paramStr = [NSString stringWithFormat:@"myAppTest://username=%@&age=%@&address=%@", @"test123", @"100", @"上海市"];
- NSURL *url = [NSURL URLWithString:[paramStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
- [[UIApplication sharedApplication] openURL:url];
这段代码来跳转目标应用并传递参数。
4,参数的接收
那么作为一个Provider怎么去接收Customer传递过来的参数呢?
首先,在找到项目中的AppDelegate.m文件,然后找到openURL方法(如果没有就去实现它)。OK,到这里你已经完成了90%了,接着继续
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
- {
- NSString *urlStr = [url absoluteString];
- if ([urlStr hasPrefix:@"myAppTest://"]) {
- NSLog(@"TestAppDemo1 request params: %@", urlStr);
- urlStr = [urlStr stringByReplacingOccurrencesOfString:@"myAppTest://" withString:@""];
- NSArray *paramArray = [urlStr componentsSeparatedByString:@"&"];
- NSLog(@"paramArray: %@", paramArray);
- NSMutableDictionary *paramsDic = [[NSMutableDictionary alloc] initWithCapacity:0];
- for (int i = 0; i < paramArray.count; i++) {
- NSString *str = paramArray[i];
- NSArray *keyArray = [str componentsSeparatedByString:@"="];
- NSString *key = keyArray[0];
- NSString *value = keyArray[1];
- [paramsDic setObject:value forKey:key];
- NSLog(@"key:%@ ==== value:%@", key, value);
- }
- }
- return NO;
- }