APP 应用之间跳转、传值

引言:

实现app之间的跳转需要注意两方面: 1.实现跳转的代码; 2. 配置url及url白名单。


# 一. 配置url

创建两个工程分别叫MyApp和YourApp, 设置MyApp的url,设置的方法有两种都是等效的,如下图:

在视图中设置:

在代码中设置:


# 二. 设置MyApp的白名单

info.plist中添加LSApplicationQueriesSchemes的数组,其中有含有一个string类型的数据,即为你要跳转的另外一个App的url,我们要从MyApp跳转到YourApp,我们把YourApp的url设置为your,所以这里要写入YourApp的url“your” :


# 三. 从MyApp跳转到YourApp

- (IBAction)gotoYoueApp:(UIButton *)sender {
    // 1.获取application对象
    UIApplication *app = [UIApplication sharedApplication];

    // 2.创建要打开的应用程序的URL
    NSURL *url = [NSURL URLWithString:@"your://"];

    // 3.判断是否可以打开另一个应用
    if ([app canOpenURL:url]) {
        // 能,就打开
        [app openURL:url];
    }else{
        NSLog(@"打开应用失败");
    }
}

# 四. 从My跳转到Your的指定页面

# 1. MyApp跳转:

- (IBAction)gotoYoueApp:(UIButton *)sender {
    // 1.获取application对象
    UIApplication *app = [UIApplication sharedApplication];

    // 2.创建要打开的应用程序的URL
    NSURL *url = [NSURL URLWithString:@"your://aaa"]; // 记得传标记

    // 3.判断是否可以打开另一个应用
    if ([app canOpenURL:url]) {
        // 能,就打开
        [app openURL:url];
    }else{
        NSLog(@"打开应用失败");
    }
}

# 2. YourApp接收:

//用于响应从URL跳转过来的方法
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    //获取window中加载的根视图,也就是那个导航
    UINavigationController *navi = (UINavigationController *)self.window.rootViewController;
    ViewController *vc = (ViewController *)navi.topViewController;

    //1.获取请求的URL地址
    NSString *absoluteStr = [url absoluteString];
    NSString *host = url.host; // 获取标记

    //2.判断地址中包含的信息为bbb则打开第二个页面
    if ([urlString hasPrefix:@"your://bbb"])
    {
        [vc performSegueWithIdentifier:@"pushWhiteSegue" sender:nil];
    }
    return YES;
}

# 五. 从Your使用跳转url的方式跳回My

- (IBAction)goBackMyApp:(id)sender {
    UIApplication *app = [UIApplication sharedApplication];
    NSURL *url = [NSURL URLWithString:@"my://"];
    if ([app canOpenURL:url]) {
        [app openURL:url];
    }else{
        NSLog(@"跳回到myapp失败");
    }
}

# 六. 常用的URL Scheme

# 1. 系统自带

名称 URL Scheme
Safari: http://
maps: http://maps.google.com
Phone: tel:
SMS: sms:
Mail: mailto:
iBooks: ibooks://
App Store: http://itunes.apple.com
iTunes: http:http://phobos.apple.com
Music / iPod: music:
Videos: videos:

# 2. 第三方软件

名称 URL Scheme
QQ: mqq://
微信: weixin://
腾讯微博: TencentWeibo://
淘宝: taobao://
支付宝: alipay://
微博: sinaweibo://
weico微博: weico://
QQ浏览器: mqqbrowser://
uc浏览器: ucbrowser://
海豚浏览器: dolphin://
欧朋浏览器: ohttp://(后要接地址,不然出错)
搜狗浏览器: SogouMSE://
百度地图: baidumap:// bdmap://
Chrome: googlechrome://
优酷: youku://
京东: openapp.jdmoble://
人人: renren://
美团: imeituan://
1号店: wccbyihaodian://
我查查: wcc://
有道词典: yddictproapp://
知乎: zhihu://
点评: dianping:// dianping://search
微盘: sinavdisk://
豆瓣fm: doubanradio://
网易公开课: ntesopen://
名片全能王: camcard://
淘宝宝贝搜索 taobao://http://s.taobao.com/?q=[prompt]
淘宝店铺搜索 taobao://http://shopsearch.taobao.com/browse/shop_search.htm?q=[prompt]