网页调起iOS APP的具体方式

31 篇文章 0 订阅
  1. 先来看看效果,这里做了三个功能
    1. 从MyApp跳转到YourApp
    2. 从MyApp跳转到YourApp的指定页面
    3. 从YourApp使用跳转url的方式跳回MyApp

  1. 实现app之间的跳转需要注意两方面

    1. 实现跳转的代码
    2. 配置url及url白名单
  2. 首先来讲url和白名单的设置

    1. 创建两个工程分别叫MyApp和YourApp
    2. 设置MyApp的url,设置的方法有两种,都是等效的,如下图

       

       

    3. 设置YourApp的url

       

    4. 设置MyApp的白名单
      在info.plist中添加LSApplicationQueriesSchemes的数组,其中有含有一个string类型的数据,即为你要跳转的另外一个App的url,我们要从MyApp跳转到YourApp,我们把YourApp的url设置为your,所以这里要写入YourApp的url“your”

       

      800567-20160325105115917-1831636091.png

    5. 设置YourApp的白名单,我写的demo中有添加用url跳转回MyApp的功能所以要设置YourApp的白名单,如果没有的话是不用设置的,系统自带的有跳转回初始APP的方法

       

    6. 注意不要把白名单的key写错了
  3. 实现跳转的代码

    1. MyApp中的代码

 

- (IBAction)gotoYoueApp:(UIButton *)sender {
    // 1.获取application对象
    UIApplication *app = [UIApplication sharedApplication];
    
    // 2.创建要打开的应用程序的URL
    NSURL *url = [NSURL URLWithString:@"your://aaa"];
    
    // 3.判断是否可以打开另一个应用
    if ([app canOpenURL:url]) {
        // 能,就打开
        [app openURL:url];
    } else {
        NSLog(@"打开应用失败");
    }
}
  1. YourApp中的代码
    写在AppDelegate.m中

 

//如果是通过URL打开的本应用,则下面的方法会被执行
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    //获取window中加载的根视图,也就是那个导航
    UINavigationController *navi = (UINavigationController *)self.window.rootViewController;
    //为了触发push第二个界面的segue,需要拿到
    //左边灰色背景的那个控制器的引用
    //而灰色的那个控制器是navi的根视图控制器
    //vc代表灰色的那个界面
    ViewController *vc = (ViewController *)navi.topViewController;
    
    //1.获取请求的URL地址
    NSString *urlString = [url absoluteString];
    
    //2.判断地址中包含的信息为bbb则打开第二个页面
    if ([urlString hasPrefix:@"your://bbb"])
    {
        [vc performSegueWithIdentifier:@"pushWhiteSegue" sender:nil];
    }
    
    return YES;
}


//新的用于响应从URL跳转过来的方法
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    //获取window中加载的根视图,也就是那个导航
    UINavigationController *navi = (UINavigationController *)self.window.rootViewController;
    //为了触发push第二个界面的segue,需要拿到
    //左边灰色背景的那个控制器的引用
    //而灰色的那个控制器是navi的根视图控制器
    //vc代表灰色的那个界面
    ViewController *vc = (ViewController *)navi.topViewController;
    
    //1.获取请求的URL地址
    NSString *urlString = [url absoluteString];
    
    //2.判断地址中包含的信息为bbb则打开第二个页面
    if ([urlString hasPrefix:@"your://bbb"])
    {
        [vc performSegueWithIdentifier:@"pushWhiteSegue" sender:nil];
    }
    return YES;
}

 Swift:

override func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        print("scheme:")
        print(url.scheme)
        print("query:")
        print(url.query)
        print("调用方app的bundle id:")
        print(options[UIApplicationOpenURLOptionsKey.sourceApplication])
        
        
        // 要与main.dart中一致
        let controller:FlutterViewController = window?.rootViewController as! FlutterViewController
        var flutterChannel = FlutterMethodChannel(name: "com.zp.mall/fromIOSorAndroid", binaryMessenger: controller as! FlutterBinaryMessenger)
        flutterChannel.invokeMethod("sendToFlutter", arguments: url.query)
        return true
    }
  1. 想要跳转到MyApp的时候

 

- (IBAction)goBackMyApp:(id)sender {
    UIApplication *app = [UIApplication sharedApplication];
    NSURL *url = [NSURL URLWithString:@"my://"];
    if ([app canOpenURL:url]) {
        [app openURL:url];
    }else{
        NSLog(@"跳回到myapp失败");
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值