【问题标题】:What is the alternative to WebIntent on iOS in Ionic 4?Ionic 4 中 iOS 上的 WebIntent 的替代方案是什么?
【发布时间】:2019-12-06 10:52:13
【问题描述】:

我需要打开一个应用程序并获取它在选择后返回的响应。我正在使用适用于 android 的 WebIntent 插件 (https://ionicframework.com/docs/native/web-intent) 进行操作,并且可以正常工作。现在我正在尝试为 iOS 做同样的事情,我正在使用 InAppBrowser (https://ionicframework.com/docs/native/in-app-browser) 但有一个错误,如果您使用 target: "_system" 打开带有链接的应用程序,则不会触发事件侦听器。在 iOS 中还有其他方法可以做到这一点吗?

WebIntent 示例:

      const options = {
        action: this.webIntent.ACTION_VIEW,
        url: 'app://url',
        extras: {
          my_extras: 'extras'
        }
      };

      this.webIntent.startActivityForResult(options).then((intent) => {
        if (intent.extras.resultCode === -1) {
          this.doSomething(intent.extras);
        } else {
          // handle errors
        }
      }, (error) => {
        // handle case when app is not installed
      });

我尝试使用 InAppBrowser 的示例

      let browser = this.iab.create('app://url', '_system');

      browser.on('exit').subscribe((event) => {
        console.log('exit'); // this event is fired actually
      }, err => {
        console.log(err);
      });

      browser.on('message').subscribe(event => {
        console.log('message'); // all other events like this one are not fired
      }, err => {
        console.log(err);
      });

感谢您的帮助!

-- 编辑--
所以我在网上找到了一种在 InAppBrowser 中使用带有 _system 目标的侦听器的方法。您必须使用 _blank 目标创建浏览器,然后使用 _system 目标在事件处理程序中重新打开它。 示例:

openBrowser() {
  const url = 'app://url'
  const browser = this.iab.create(url, '_blank');
  browser.on('beforeload').subscribe(evt => {
    this.beforeloadEventHandler(evt, url);
  }
  // close the blank browser where you want
  browser.close();
}
public beforeloadEventHandler(event, url) {
  const browser = this.iab.create(url, '_system');
  browser.on('beforeload').subscribe(evt => {
    this.beforeloadEventHandler(evt, url);
  });

  // your logic
  console.log('beforeload evt');
  console.log(JSON.stringify(event));
}

但最后这个解决方案对我不起作用,因为我没有通过 InAppBrowser 侦听器获取任何数据,我想还有另一种方法可以做到这一点。

【问题讨论】:

    标签: ios ionic4 inappbrowser


    【解决方案1】:

    一个简单的替代方法是使用电容器 API

    App.addListener('appUrlOpen', (data: any) => {
        // data.url contains the url that is opening your app
        // so you can use it to check what to do
    });
    
    const canOpen = await App.canOpenUrl({ url: 'app://url' });
    if (canOpen.value === true) {
        // can open 'app://url'
    }
    const open = await App.openUrl({ url: 'app://url' });
    if (!open.completed) {
        // app not opened, custom logic like ask to install the app or redirect to the store
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 2013-10-12
      • 2016-10-18
      • 2022-11-10
      • 2021-11-13
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      相关资源
      最近更新 更多