iOS两个App应用之间的跳转

开发IOS项目的时候,有可能会遇到两个APP应用相互调用的需求。

下面来详细介绍实现的步骤:

 

1,在你的目标应用程序里添加URL Types项

a,打开项目中info.plist文件,在infomation property list项下面增加一项URL Typs

b,展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme

c,展开URL Scheme,将Item1的内容修改为myapp

(此为跳转的key 其他应用可通过”myapp://“来访问此自定义URL的应用程序)

 

2.在原程序里面 添加跳转代码

在你需要跳转的地方写

NSString *paramStr = [NSString stringWithFormat:@"myAppTest://username=%@&age=%@&address=%@", @"AppSwitch", @"100", @"上海市"];
    NSURL *myURL_APP_A = [NSURL URLWithString:[paramStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    if ([[UIApplication sharedApplication] canOpenURL:myURL_APP_A]) {
        NSLog(@"canOpenURl");
        [[UIApplication sharedApplication] openURL:myURL_APP_A];
    } else {
        NSLog(@"没有安装该应用程序");
    }

  "username=%@&age=%@&address=%@" 为你需要传递的参数

 

 

3.在目标应用程序接收参数

 

那么作为一个Provider怎么去接收Customer传递过来的参数呢?

 

首先,在找到项目中的AppDelegate.m文件,然后找到openURL方法(如果没有就去实现它)。OK,到这里你已经完成了90%了,接着继续

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    
    NSLog(@"从%@跳转过来的 - %@",sourceApplication,[url resourceSpecifier]);
    
    NSString *urlStr = [[url absoluteString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    if ([urlStr hasPrefix:@"myAppTest://"]) {
        urlStr = [urlStr stringByReplacingOccurrencesOfString:@"myAppTest://" withString:@""];
        NSArray *paramArray = [urlStr componentsSeparatedByString:@"&"];
        NSMutableDictionary *paramsDic = [[NSMutableDictionary alloc] initWithCapacity:0];
        NSMutableString *message = [[NSMutableString alloc] init];
        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];
            [message appendFormat:@"%@==%@\n",key,value];
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"获取到的参数" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
    }
    return YES;
}

  

通过本身自定的参数拼接规则,来解析参数。

到这里已经完成了应用之间的跳转,怎么样是不是很简单?

如果需要 回跳  方法一样  注意:这里需要用真机调试

 

posted @ 2015-10-15 12:33  Maple023  阅读(269)  评论(0编辑  收藏  举报