zoukankan      html  css  js  c++  java
  • iOS

    一个程序若要跳到另一个程序。需要在目标程序的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打头即可

     

     

     

     

  • 相关阅读:
    UARTRS232与RS485的关系
    RS-485总线通信协议
    【转】TCP(协议号6)的方方面面
    【转】Android 编程下的代码混淆
    【转】阻塞与非阻塞socket的优缺点
    【转】Socket状态变迁图
    【转】Spring中IoC的优点与缺点
    linux_UAPI_转
    数组初始化
    中兴被制裁:到底对谁说话 13亿人民中1人有话说
  • 原文地址:https://www.cnblogs.com/luorende/p/5811463.html
Copyright © 2011-2022 走看看