zoukankan      html  css  js  c++  java
  • (八十五)应用程序间的跳转与消息传递

    应用程序的跳转识别的是URL的协议头,每个应用都可以指定一个URL的协议头,以此作为跳转的依据,而URL的地址部分作为消息体。

    【指定应用程序URL协议头的方法】

    选择TARGETS->info->URL Types,添加URL Schemes:

    【实现跳转的方法】

    要实现应用级操作,需要借助UIApplication单例的openURL方法。

    加入A要跳转到B,B的URL Schemes为app2,则实现跳转的URL应该为"app2://...",...部分随意,为要传递的消息。

    注意在跳转前先用canOpenURL判断是否有这个app,没有应该打开App Store推荐用户下载。

    UIApplication *app = [UIApplication sharedApplication];
    NSURL *url = [NSURL URLWithString:@"app2://"]; // 有协议头即可打开app,后面的内容用于指示应该打开app的哪个功能。
    if ([app canOpenURL:url]) {
        [app openURL:url];
    }else{
        NSLog(@"根据App id打开App Store");
    }

    【接收跳转的方法】

    在AppDelegate中有两个方法可以实现在跳转到自己后接收URL:

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
        return YES; // 代表是否成功处理   
    }
    
    
    // 新方法会使得旧方法失效,建议两个都写
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
        return YES; 
    }
    第二个方法比较新,有sourceApplication参数来得到执行跳转的App的Bundle identifier,其中url就是执行跳转时打开的url,建议两个方法都实现。

    【利用两个App演示应用跳转与消息传递】

    App1:两个按钮,第一个点击后跳转到App2的主页,第二个点击后跳转到App2的另一个页面page1。

    App2:有主页和page1和page2两个页面,通过NavigationController跳转。

    ①App1中实现跳转到App2的page1的代码:

    // 应用级操作利用UIApplication单例完成
    UIApplication *app = [UIApplication sharedApplication];
    NSURL *url = [NSURL URLWithString:@"app2://page=1"]; // 有协议头即可打开app,后面的内容用于指示应该打开app的哪个功能。
    if ([app canOpenURL:url]) {
        [app openURL:url];
    }else{
        NSLog(@"根据App id打开App Store");
    }
    ②App2中实现接收与页面跳转的方法:

    在App2中,界面的关系是NavigationController->Home(show方式连接的page1和page2,page1和page2的segue分别为page1和page2)。

    如果要实现给控制器传值,可以在performSegueWithIdentifier方法传入sender,在跳转前还会调用prepareForSegue::方法拿到sender,利用segue的sourceViewController和destinationViewController来赋值。

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
        
        NSLog(@"url = %@ id = %@",url.absoluteString,sourceApplication);
        
        // 为了实现跳转,应该拿到导航控制器,拿到首页的控制器,然后调用performSegueWithIdentifier::
        // 调用performSegueWithIdentifier跳转前会调用prepareForSegue,用来给要跳到的控制器传值。
        UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
        
        ViewController *homeVc = (ViewController *)nav.topViewController;
        
        NSLog(@"%@",url);
        
        NSString *urlStr = url.absoluteString;
        if ([urlStr rangeOfString:@"page=1"].location != NSNotFound) {
            NSLog(@"跳转到页面1");
            if (![homeVc isKindOfClass:[ViewController class]]) return YES;
            [homeVc performSegueWithIdentifier:@"page1" sender:nil];
        }else{
            NSLog(@"跳转到页面2");
            if (![homeVc isKindOfClass:[ViewController class]]) return YES;
            [homeVc performSegueWithIdentifier:@"page2" sender:nil];
        }
        
        return YES;
        
    }

  • 相关阅读:
    .Net Core微服务——Ocelot(2):集成Consul 老马
    .NET 微服务——CI/CD(1):Jenkins+Gitee自动构建 老马
    .Net Core——用SignalR撸个游戏 老马
    JUC之线程间的通信
    SpringBoot文章合集
    JUC之线程间定制化通信
    JUC之集合中的线程安全问题
    JUC文章合集
    JUC之Lock接口以及Synchronized回顾
    JUC概述
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154114.html
Copyright © 2011-2022 走看看