Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript / Clipboard API / Safari iOS / NotAllowedError Message

I have a short Problem. I want to add a text to the clipboard on my iOS Device when I press a Button which use a ajax request to get the data into the clipboard.

With the Browser on my Desktop and my Android device it works very well but with my iOS Device a get an Issue.

$.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);
                        });

So I get following error message:

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

How can I get my Text to the clipboard? I use iOS 13.5 and 13.4 should support the Clipboard API.

like image 873
Daniel Avatar asked Dec 23 '22 17:12

Daniel


1 Answers

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: The implementation is available through the navigator.clipboard API which must be called within user events

You are trying to copy to the clipboard as a result of an API call instead of in the context of a user interaction. This is not allowed.

like image 198
Jordan Papaleo Avatar answered Jan 13 '23 17:01

Jordan Papaleo