開發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;
}
通過本身自定的參數拼接規則,來解析參數。
到這裏已經完成了應用之間的跳轉,怎麼樣是不是很簡單?