Ionic 4 中 iOS 上 WebIntent 的替代方案是什么?
What is the alternative to WebIntent on iOS in Ionic 4?
我需要打开一个应用程序并在选择后得到它的响应 returns。我正在使用 WebIntent 插件 (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) 执行此操作,但存在一个错误,如果您使用 target: "_system" 打开带有 link 的应用程序,则不会触发事件侦听器。在 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 侦听器取回任何数据,我想还有另一种方法可以做到这一点。
一个简单的替代方法是使用电容器 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
}
我需要打开一个应用程序并在选择后得到它的响应 returns。我正在使用 WebIntent 插件 (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) 执行此操作,但存在一个错误,如果您使用 target: "_system" 打开带有 link 的应用程序,则不会触发事件侦听器。在 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 侦听器取回任何数据,我想还有另一种方法可以做到这一点。
一个简单的替代方法是使用电容器 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
}