iOS

来源:互联网 发布:ubuntu 传输文件 编辑:程序博客网 时间:2024/05/19 13:09

一个程序若要跳到另一个程序。需要在目标程序的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 = [UIButtonbuttonWithType:UIButtonTypeCustom];

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

    button.backgroundColor = [UIColordarkGrayColor];

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

    button.titleLabel.font = [UIFontsystemFontOfSize:20];

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

    [self.viewaddSubview:button];

}

//跳转到SecondApp

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

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

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

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

    NSURL * url = [NSURLURLWithString:UrlStr];

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

    if ([[UIApplicationsharedApplication]canOpenURL:url]) {

        [[UIApplicationsharedApplication] openURL:url];

    }else{

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

    }  

}

//跳转到AppStore

-(void)abc{

    [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@""]];

    [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@""]]

}

 

 

 

  第二个工程 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 = [UIButtonbuttonWithType:UIButtonTypeCustom];

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

    button.backgroundColor = [UIColordarkGrayColor];

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

    button.titleLabel.font = [UIFontsystemFontOfSize:20];

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

    [self.viewaddSubview:button];

    

}

 

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

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

    

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

    NSURL * url = [NSURLURLWithString:UrlStr];

    

    if ([[UIApplicationsharedApplication]canOpenURL:url]) {

        [[UIApplicationsharedApplication] openURL:url];

    }else{

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

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

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

        

        alertView.alertViewStyle =UIAlertViewStyleDefault;

        

        [alertView show];

        

    }

 

 

  注: 另外说明一下

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

 

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

 

 

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

 

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

 

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

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