技术咨询、项目合作、广告投放、简历咨询、技术文档下载 点击这里 联系博主

导语:在使用 APP 的过程中,我们肯定遇到过从 A APP 跳转到 B APP 指定页面的场景,那么这个是怎么做到的呢?其实就是使用了 URLSchema。

# 一、URLSchema 跳转 App Store

使用 URLSchema 跳转其实和我们之前打开电话,短信一样都是使用openURL打开的;其次 每一个 app 在 app store 都是有一个唯一的路径,我们可以使用这个唯一标识符跳转到指定的 url

此处拿 APP 跳转到 app store 为例:


    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"对App进行评分" message:@"请给予一个好评" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        //URL Scheme 必须正确
        NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/app/iqiyi"];

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

            [UIApplication.sharedApplication openURL:url options:@{} completionHandler:nil];
        }

    }];

    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];


    [alert addAction:ok];

    [alert addAction:cancel];

    [self presentViewController:alert animated:YES completion:nil];

# 二、系统设置页面跳转


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //2. iOS10只允许如下方式跳转到设置里自己app的界面,对跳转到其他界面做了限制:
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];


}

@end

# 三、自定义 Schema 和 APP 指间的跳转

前面我们学习了如何使用 Schema 的方式跳转到 app store 和系统设置页面,那么我们如何实现自定义的 schema 跳转到指定的 app 页面呢?

由于 app 的跳转时按照 schema 的方式跳转,所以我们首先的给自己的 app 设置 schema。

# 1. 为自定义 app 设置 schema

本次案例从 A 跳转到 B,所以首先需要给 B app 设置 schema,

仅仅设置 schema 还是不够的,还需要给 A app 设置可以查询的白名单权限:

在 A App 中的 Info.plist 中新增:

<key>LSApplicationQueriesSchemes</key>
<array>
	<string>bAPP</string>
</array>

# 2. 跳转到指定的 app


//打开一个App
- (IBAction)openApp:(id)sender {

    // 1.获取应用程序App-B的URL Scheme
    NSURL *appUrl = [NSURL URLWithString:@"bApp://"];

    // 2.判断手机中是否安装了对应程序
    if ([[UIApplication sharedApplication] canOpenURL:appUrl]) {
        // 3. 打开应用程序App-B
        [[UIApplication sharedApplication] openURL:appUrl options:@{} completionHandler:nil];

    } else {

        NSLog(@"没有安装");

    }

    //3.iOS9以后需要设置白名单,如果使用 canOpenURL:方法,该方法所涉及到的 URL Schemes 必须在"Info.plist"中将它们列为白名单,否则不能使用。key叫做LSApplicationQueriesSchemes ,键值内容是对应应用程序的URL Schemes。不论跳转到App还是具体的界面(app://或者app://abc),只需要设置一次


}

# 3. 跳转到指定页面

实现的主要步骤:

  • A APP 构造 schema 并使用 openURL 打开 B APP
  • B APP 解析 URL,执行页面跳转

A APP 跳转 B APP

// A APP跳转,将参数放在url后面
- (IBAction)openSpecial:(id)sender {


    // 1.获取应用程序App-B的URL Scheme
    NSURL *appUrl = [NSURL URLWithString:@"testApp://VC1"];

    // 2.判断手机中是否安装了对应程序
    if ([[UIApplication sharedApplication] canOpenURL:appUrl]) {
        // 3. 打开应用程序App-B
        [[UIApplication sharedApplication] openURL:appUrl options:@{} completionHandler:nil];

    } else {

        NSLog(@"没有安装");

    }
}

B APP 根据传递的参数跳转到指定的页面

其主要的方法需要重写AppDelegate.m中的openURL方法:

// AppDelegate.m

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    
    // 拿到当前App的第一个界面
    UIViewController *main = app.keyWindow.rootViewController;
    
    // 根据字符串关键字来跳转到不同页面
    if ([url.absoluteString containsString:@"VC1"]) { // 跳转到应用App-B的Page1页面
        // 根据segue标示进行跳转或者使用其他的方式,详情请见  https://mrgaogang.github.io/ios/controller/UIViewController%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3.html
        
        [main performSegueWithIdentifier:@"abc" sender:nil];
    }
    
    return YES;
}


【未经作者允许禁止转载】 Last Updated: 2/4/2024, 6:06:40 AM