admin管理员组文章数量:1426802
So, we are implementing navigator.share()
() in an Ionic v3 PWA.
It works, but there's a little problem we encountered which we don't know how to fix: when the navite share selector pops up (user can select betwen inbox, gmail, twitter, etc) and the user then presses the back button on android (dismissing the native modal that came up), the function doesn't trigger any response in the promise. Not success, not error.
The problem here is that we show a loading spinner before we call the navigator.share function and if the user presses that back button at that precise moment, we can't trigger the function that hides the loading spinner.
This is the part of the code where we implement the feature:
public share(title: string, message: string, url: string) {
// if it's mobile & web (ie: chrome on android)
if (this.isMobileWeb === true) {
this.showLoading();
if ((navigator as any).share) {
(navigator as any).share({
title,
message,
url,
})
.then(() => {
console.log('Success share');
this.hideLoading();
})
.catch((error) => {
console.log('Error share', error);
this.hideLoading();
});
}
else {
console.log('navigator.share() not supported.');
this.hideLoading();
}
}
}
With this implementation, no success or error is thrown if the user presses the back button when the native share modal es up. Is there something wrong with this code?
I've also tried adding another callback function inside the .then()
function, as in: .then(successFn, errorFn)
. The errorFn
is never called either.
Thanks for your time.-
So, we are implementing navigator.share()
(https://developers.google./web/updates/2016/09/navigator-share) in an Ionic v3 PWA.
It works, but there's a little problem we encountered which we don't know how to fix: when the navite share selector pops up (user can select betwen inbox, gmail, twitter, etc) and the user then presses the back button on android (dismissing the native modal that came up), the function doesn't trigger any response in the promise. Not success, not error.
The problem here is that we show a loading spinner before we call the navigator.share function and if the user presses that back button at that precise moment, we can't trigger the function that hides the loading spinner.
This is the part of the code where we implement the feature:
public share(title: string, message: string, url: string) {
// if it's mobile & web (ie: chrome on android)
if (this.isMobileWeb === true) {
this.showLoading();
if ((navigator as any).share) {
(navigator as any).share({
title,
message,
url,
})
.then(() => {
console.log('Success share');
this.hideLoading();
})
.catch((error) => {
console.log('Error share', error);
this.hideLoading();
});
}
else {
console.log('navigator.share() not supported.');
this.hideLoading();
}
}
}
With this implementation, no success or error is thrown if the user presses the back button when the native share modal es up. Is there something wrong with this code?
I've also tried adding another callback function inside the .then()
function, as in: .then(successFn, errorFn)
. The errorFn
is never called either.
Thanks for your time.-
Share Improve this question asked Apr 5, 2018 at 2:28 ArielAriel 1,5272 gold badges20 silver badges30 bronze badges 1- I have a similar problem. The promise gets resolved right away when user shares, but it doesn't get rejected right away when user cancels. It will get rejected whenever share menu is opened again though. – ydou Commented Aug 7, 2018 at 22:40
1 Answer
Reset to default 5This is a long-standing bug in Android. Here is my solution for it, but I can't take credit for the technique. It was mentioned in the bug tracker linked below. Pardon the use of ES6 syntax. The idea is that you would call this share function instead of the 'navigator.share' function directly.
export default options => new Promise((resolve, reject) => {
navigator.share(options).then(resolve).catch(error => {
// Differentiate between user 'AbortError' and internal errors.
// E.g. Internal error: could not connect to Web Share interface.
if (error.message.startsWith('Internal error:'))
error.name = 'InternalError';
reject(error);
});
/*
* https://bugs.chromium/p/chromium/issues/detail?id=636274
* If the share promise is not resolved or rejected when focus is returned to
* the window, then reject it after a 100ms delay.
*/
let cancel = () => setTimeout(() => {
window.removeEventListener('focus', cancel);
let error = new Error('Share cancelled');
error.name = 'ShareTimeout';
reject(error);
}, 100);
window.addEventListener('focus', cancel);
});
本文标签:
版权声明:本文标题:javascript - navigator.share won't resolve nor reject when user cancels native selector on android - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745485657a2660371.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论