Javascript/剪贴板 API/Safari iOS/NotAllowedError 消息

标签 javascript ios api safari clipboard

我有一个简短的问题。当我按下一个使用 ajax 请求将数据获取到剪贴板的按钮时,我想在我的 iOS 设备上的剪贴板上添加一个文本。

使用我桌面上的浏览器和我的 Android 设备,它运行良好,但使用我的 iOS 设备时出现问题。

$.ajax({
            type: "GET",
            url: "missing_string.php",
            data: getAttributes($(this)),
            success: function(data) {
                if (navigator.clipboard) {
                    navigator.clipboard.writeText(data)
                        .then(() => {
                            alert("SUCCESS");
                        })
                        .catch(err => {
                            alert(err);
                            console.log('Something went wrong', err);
                        });

所以我收到以下错误消息:

NotAllowedError: the request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.



如何让我的文本到剪贴板?我使用 iOS 13.5 和 13.4 应该支持剪贴板 API。

最佳答案

https://webkit.org/blog/10247/new-webkit-features-in-safari-13-1/

Async Clipboard API
WebKit brings the Async Clipboard API to this release of Safari...

you should read it
TLDR:
该实现可通过 navigator.clipboard 获得。必须在用户事件中调用的 API
您正试图通过 API 调用而不是在用户交互的上下文中复制到剪贴板。这是不允许的。

关于Javascript/剪贴板 API/Safari iOS/NotAllowedError 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62327358/

相关文章:

javascript - Google Maps API 是否适用于 Github Pages?

javascript - play框架和requirejs开发javascript代码

ios - 在 ios 中开发多模块(框架)项目

android - 如何在 Xamarin android 和 ios 中休眠线程?

ios - Swift:如何注销保留对象数?

android - 如何对低于 L 的 Android 版本使用 startLockTask() 方法

api - Laravel 5.1 Guzzle - undefined offset : 0

javascript - 如何在表单中使用 JavaScript?

javascript - 访问使用 AJAX 注入(inject)的 html 元素的属性

javascript - navigator.language 在 app.js 中无法正确识别