iOS应用唤醒与交互

实现应用之间的跳转通信需要了解最重要的是URL Schema

第一:什么是URL Scheme

本来我想说几句我的理解,后来看到了网上别人的见解之后,发现自己确实是麻瓜,文章网址:http://sspai.com/31500/
个人目前的理解URL Scheme就是为了应用之间通信和交互而存在的
现在举个例子说说怎么用呢

第二:怎么用

1.创建应用A,应用A为源应用,它将跳转到应用B,并实现双方的应用传值通信
首先明确的是B应用是否存在,如果B应用不存在则跳转AppStore下载B应用,否则唤醒B应用并跳转到B应用
对应用A进行配置
应用A的URL Scheme:ApplicationA
应用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
应用A的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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章