實現應用之間的跳轉通信需要了解最重要的是URL Schema
第一:什麼是URL Scheme
本來我想說幾句我的理解,後來看到了網上別人的見解之後,發現自己確實是麻瓜,文章網址:http://sspai.com/31500/
個人目前的理解URL Scheme就是爲了應用之間通信和交互而存在的
現在舉個例子說說怎麼用呢
第二:怎麼用
1.創建應用A,應用A爲源應用,它將跳轉到應用B,並實現雙方的應用傳值通信
首先明確的是B應用是否存在,如果B應用不存在則跳轉AppStore下載B應用,否則喚醒B應用並跳轉到B應用
對應用A進行配置
應用A點擊按鈕跳轉應用B,並嚮應用B傳遞參數
- (IBAction)skipAction
{
//判斷應用B有沒有存在,不存在就下載
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@”ApplicationB://”]]) {
//已經存在應用b,跳轉並傳值
NSString *paramStr = [NSString stringWithFormat:@”ApplicationB://username=%@&age=%@&address=%@”, @”test123”, @”100”, @”上海市”];
NSURL *url = [NSURL URLWithString:[paramStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication]openURL:url];
}else{
//跳轉到AppStore下載
NSString *str = @"itms-apps://itunes.apple.com/cn/app/e-er-duo-si-shi-zhong-xin-yi-yuan/id1041292260?mt=8";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];}
}
2.創建應用B,並配置應用B的URL Scheme
在應用B代理方法裏面接收A應用傳來的參數
- (BOOL)application:(UIApplication )application openURL:(NSURL )url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSString *urlStr = [url absoluteString];
if ([urlStr hasPrefix:@"ApplicationB://"]) {
urlStr = [urlStr stringByReplacingOccurrencesOfString:@"ApplicationB://" 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);
}
NSString *message =[NSString stringWithFormat:@"%@",paramArray];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"A傳過來的值" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
[alertView show];
}
return NO;}
iOS9.0
// 以後使用新API接口- (BOOL)application:(UIApplication )app openURL:(NSURL )url options:(NSDictionary