iOS - (两个APP之间的跳转)

一个程序若要跳到另一个程序。需要在目标程序的plist文件里面修改:

打开info.plist,添加一项URL types

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

展开URL Scheme,将Item0的内容修改为 SecondApp(此为跳转的key

 

 

 

 

话不多说,下面开始讲解步骤:

  首先创建两个工程,第一个 FirstAPP , 第二个 SecondAPP

 

第一个 First APP 的 info.plist 需要设置 key(url) 与 白名单

 

接下来我们再对第二个 SecondAPP 工程来做相应的处理

 

 

   将这两个工程设置好了之后,接下来上代码

  第一个 FirstApp工程

//

//  ViewController.m

//  FirstAPP

//

//  Created by luorende on 16/8/25.

//  Copyright © 2016 luorende. All rights reserved.

//

 

#import "ViewController.h"

@interface ViewController ()

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(100, 100, 200, 50);

    button.backgroundColor = [UIColor darkGrayColor];

    [button setTitle:@"跳转到SecondApp" forState:UIControlStateNormal];

    button.titleLabel.font = [UIFont systemFontOfSize:20];

    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}

//跳转到SecondApp

-(void)clickButton:(UIButton *)button{

    NSLog(@"执行了点击事件");

    //之前配置的白名单,就是需要跳转对方Appkey,即对方设置的url

    NSString * UrlStr = @"SecondApp://xxxxx";

    NSURL * url = [NSURL URLWithString:UrlStr];

    // 在这里可以先做个判断

    if ([[UIApplication sharedApplication]canOpenURL:url]) {

        [[UIApplication sharedApplication] openURL:url];

    }else{

        NSLog(@"应用程序未安装");

    }  

}

//跳转到AppStore

-(void)abc{

    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@""]];

    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@""]]

}

 

 

 

  第二个工程 SecondAPP 里的代码

 

//

//  ViewController.m

//  SecondAPP

//

//  Created by luorende on 16/8/26.

//  Copyright © 2016 luorende. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(100, 100, 200, 100);

    button.backgroundColor = [UIColor darkGrayColor];

    [button setTitle:@"SecondApp,跳转到另一个APP" forState:UIControlStateNormal];

    button.titleLabel.font = [UIFont systemFontOfSize:20];

    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

}

 

-(void)clickButton:(UIButton *)button{

    NSLog(@"执行了点击事件");

    

    NSString * UrlStr = @"FirstAPP://xxxxx";

    NSURL * url = [NSURL URLWithString:UrlStr];

    

    if ([[UIApplication sharedApplication]canOpenURL:url]) {

        [[UIApplication sharedApplication] openURL:url];

    }else{

        NSLog(@"应用程序未安装");

    // 程序未成功跳转,我们还可以做一个提示

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"应用程序未安装"message:@"确定下载<xxxx>应用吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];

        

        alertView.alertViewStyle = UIAlertViewStyleDefault;

        

        [alertView show];

        

    }

 

 

  注: 另外说明一下

例如:相互跳转的时候双方都要设置URL与白名单 ,若是 FirstAPP 不设置URL types 项(自己注册自己URL)

 

则实现的功能是:FirstAPP 可以跳转到 SecondAPP  ,但SecondAPP无法跳转过来

 

 

当然双方只设置 LSApplicationQueriesSchemes  项也是不行的,会提示应用程序未安装  (白名单)

 

简单说来 就是需要有一个要设置 URL 

 

自己设置了的话,就是说已经有了URL,别人不注册, 使用设置白名单后也能跳转

   总结:谁要跳,谁就要设置谁为白名单。  白名单要与跳到App设置的域名URL 要保持一致 另外代码部分的URL也要以域名URL打头即可

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值