iOS两个APP之间的跳转和传值(如微信登录、支付跳转)

在iOS中,可以使用openUrl从一个APP跳转到另一个APP。

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

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

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

展开URL Scheme,将Item0的内容修改为Petsoto(此为自己的URL types的key,如果其他APP想要跳到你的APP,只需对方把自己这个Key加到白名单里边就可以了)。

 

 

 

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

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

 

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

 

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

 

First APP中的按钮,执行下面的方法:

- (IBAction)buttonAction:(id)sender {
    
    NSLog(@"执行了点击事件");
    
    //之前配置的白名单,就是需要跳转对方App的key,即对方设置的url
    // xxxxx :可以将参数拼接在url后边,这样在另一个APP的openUrl方法中,解析这个url中的参数就可以了
    NSString * UrlStr = @"SecondApp://xxxxx";
    
    NSURL * url = [NSURL URLWithString:UrlStr];
    
    // 在这里可以先做个判断
    if ([[UIApplication sharedApplication]canOpenURL:url]) {
        
        [[UIApplication sharedApplication] openURL:url options:nil completionHandler:nil];
        
    }else{
        
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"跳转的应用程序未安装" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        
        [alert show];
    }  
    
}

SecondAPP中的按钮,执行下面的方法:

- (IBAction)buttonAction:(id)sender {
    
    NSLog(@"执行了点击事件");
    
    NSString * UrlStr = @"FirstAPP://xxxxx";
    NSURL * url = [NSURL URLWithString:UrlStr];
    
    if ([[UIApplication sharedApplication]canOpenURL:url]) {
        
        [[UIApplication sharedApplication] openURL:url options:nil completionHandler:nil];
        
    }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,别人不注册, 使用设置白名单后也能跳转.

到此,两个工程就可以进行互相跳转了。

如果还需要进行传值的话,将参数拼接在url后边就可以了(见上边方法中的注释部分)。 然后在下面的方法中获取传过来的参数。

#pragma
//在此方法中解析url,可以获得传过来的参数
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    
    NSString *canshu = url.absoluteString;
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"传过来的参数是" message:canshu delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
    
    return NO;
}

通过URL只能简单地传递参数, 如果要传递image,可以通过剪贴板UIPasteboard来实现.

如在FirstAPP中, 向系统剪贴板中填充数据:

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @"Modal.jpg";
pasteboard.image = [UIImage imageNamed:@"Modal.jpg"];

// NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"Modal.jpg"], 0);
// [pasteboard setData:imageData forPasteboardType:@"Modal.jpg"];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"demomixpanel://params?param1=111&param2=222"]];

则在SecondAPP中, 从剪贴板中取出数据便可以使用了

 UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
 // label.text = pasteboard.string;    
 // imageView.image = pasteboard.image;
 // NSData *imageData = [pasteboard dataForPasteboardType:@"Modal.jpg"];
 // imageView.image = [UIImage imageWithData:imageData];

两个APP之间的互相跳转和传值基本就这些了。

我只是搬运工,今天遇到有人问这个问题,特此记录下;

引用地址:https://blog.csdn.net/u013602835/article/details/52882854

1

这篇文章还没有评论

发表评论