首页 > 解决方案 > Android原生分享菜单:检测支持的浏览器

问题描述

我正在开发一个使用 webview 的 Android 应用程序,其中包含如下发布的代码。

函数 shareArticle() 旨在根据对 navigator.share(Google 的 API 调用本机共享菜单)的支持过滤浏览器并相应地执行操作。但是,单击调用该函数的按钮时没有任何反应,至少警报提示会很方便。

知道如何制作,以便可以使用原生 Android 共享屏幕共享内容吗?

export const shareArticle = () => {
    return AndroidNativeShare("Title", "www.google.com", 'description');
};

async function AndroidNativeShare(Title, URL, Description) {
    if (typeof navigator.share === 'undefined' || !navigator.share) {
        alert('Your browser does not support Android Native Share, it\'s tested on chrome 63+');
    } else if (window.location.protocol != 'https:') {
        alert('Android Native Share support only on Https:// protocol');
    } else {
        if (typeof URL === 'undefined') {
            URL = window.location.href;
        }
        if (typeof Title === 'undefined') {
            Title = document.title;
        }
        if (typeof Description === 'undefined') {
            Description = 'Share your thoughts about ' + Title;
        }
        const TitleConst = Title;
        const URLConst = URL;
        const DescriptionConst = Description;

        try {
            await navigator.share({title: TitleConst, text: DescriptionConst, url: URLConst});
        } catch (error) {
            console.log('Error sharing: ' + error);
            return;
        }
    }
}

标签: javascriptandroidhtmlreactjs

解决方案


检查是否存在navigator.share是推荐和合乎逻辑的解决方案。不幸的是,这似乎在 webviews 中不起作用。请参阅此处提交的错误:https ://bugs.chromium.org/p/chromium/issues/detail?id=765923


推荐阅读