ios - What is the alternative to WebIntent on iOS in Ionic 4?
问题描述
I need to open an App and get the response it returns after a choice. I'm doing it with the WebIntent plugin (https://ionicframework.com/docs/native/web-intent) for android and it works. Now i'm trying to do the same thing for iOS, i'm using InAppBrowser (https://ionicframework.com/docs/native/in-app-browser) but there is a bug, if you open an app with a link using target: "_system" the event listeners are not triggered. Are there other ways to do this in iOS?
Example of 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
});
Example of my try with 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);
});
Thanks for any help!
-- EDIT --
So i've found a way online to use listeners in InAppBrowser with _system target. You have to create the browser with _blank target then reopen it in the event handler with _system target.
Example:
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));
}
But in the end this solution does not work for me cause i don't get any data back with InAppBrowser listeners, i guess there is another way to do it.
解决方案
一个简单的替代方法是使用电容器 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
}
推荐阅读
- visual-studio-code - 在 VS Code 中,即使在调整了更漂亮的配置设置后,Prettier 也不会以逗号结尾
- ansible - 如何使用 ansible 创建和编辑文件
- google-apps-script - 邮件未通过 Google 表格应用脚本发送
- mysql - 如何为单个列中存在的值添加多个条件并将结果显示在单独的列中?
- reactjs - Kendo React - X 轴共享 Y 轴的图表
- bash - 使用别名在一行中运行多个 git 命令
- sql-server - 重新创建复制时的身份范围冲突
- python-3.x - pytest 由于 ModuleNotFoundError 而失败,但在使用“python -m pytest”时有效
- sql - Oracle SQL 获取上周日
- php - 格式化为特定 HTML 的递归树