【发布时间】: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