APP之间的跳转

APP之间的跳转

一、摘要

点击某个App内链接时,可以跳转到另外一个App,主要使用UIApplication和scheme

 

二、基础知识预备

网址的构成:

e g:http://www.baidu.com/1.png?pwd=123

 OC中使用    url.scheme 获取url的协议 http

          url.host 获取主机名 www.baidu.com

          url.relativePath 路径 1.png

          url.query 获取参数 pwd=123

 

三、设置App的URL schemes(非常重要)  

APP之间的跳转

 

四、设置跳转

在Qunaer项目里面的ViewController.m

- (IBAction)jump:(id)sender {
    //使用openURL方法跳转
    //1.创建地址对象
    NSURL *url = [NSURL URLWithString:@"Zhifubao://Qunaer"];
    [[UIApplication sharedApplication] openURL:url options:nil completionHandler:nil];
}

 

五、设置跳回原App

在AppDelegate.m中的- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options方法中实现:

Zhifubao项目的AppDelegate.m

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    //外部程序跳转过来 就会触发这个方法
    /*
     http://www.baidu.com/1.png?pwd=123
     url.scheme 获取url的协议 http
     url.host 获取主机名 www.baidu.com
     url.relativePath 路径 1.png
     url.query 获取参数 pwd=123
     */
    NSString *scheme = url.host;
  //解析网址
    NSURL *u = [NSURL URLWithString:[NSString stringWithFormat:@"%@://", scheme]];
    
    [[UIApplication sharedApplication] openURL:u options:nil completionHandler:nil];
    
    return  YES;
}
    //解析网址
    NSURL *u = [NSURL URLWithString:[NSString stringWithFormat:@"%@://", scheme]];
    
    [[UIApplication sharedApplication] openURL:u options:nil completionHandler:nil];
    
    return  YES;

 注意:这里的return 返回yes是尽可能跳转成功,no则是跳转失败

 

posted @ 2019-03-05 20:04 健泽 阅读(...) 评论(...) 编辑 收藏