cordova-clipboard icon indicating copy to clipboard operation
cordova-clipboard copied to clipboard

Can't access cordova.plugins.clipboard API only iOS.

Open yuto7th opened this issue 4 years ago • 7 comments

Hi team! Thanks for your good products.

I want to paste text to use this plugin. Android OS worked well. But, iOS didn't work.

Error is here. TypeError: undefined is not an object ( evaluating 'cordova.plugins.clipboard')

It's impossible to access "cordova.plugins" to use only iOS. (Android is fine.) Please teach me how to resolve this problem.

yuto7th avatar May 18 '20 14:05 yuto7th

Prefix with window, window.cordova.plugins.clipboard. Give that a try!

wuori avatar Oct 23 '20 00:10 wuori

Hi team! Thanks for your good products.

I want to paste text to use this plugin. Android OS worked well. But, iOS didn't work.

Error is here. TypeError: undefined is not an object ( evaluating 'cordova.plugins.clipboard')

It's impossible to access "cordova.plugins" to use only iOS. (Android is fine.) Please teach me how to resolve this problem.

I´m having a very similar problem, it works fine on Android, but on ios it does nothing, have you find any solution?

Martherius avatar Jan 17 '22 15:01 Martherius

@Martherius Can you show me your code implementation for the clipboard call? I'm on a newer machine and don't have everything I need set up to debug at the moment.

wuori avatar Jan 19 '22 01:01 wuori

@Martherius Can you show me your code implementation for the clipboard call? I'm on a newer machine and don't have everything I need set up to debug at the moment.

This is the code on my controller.js im working on an ionic 1 app: $scope.copyText = function(value) { console.log(value); $cordovaClipboard.copy(value).then(function() { console.log(value + " copied to clipboard!"); $scope.showAlertExito("Copiado a portapapeles."); }, function() { console.error("Error. No se pudo copiar."); }); };

Then just call the method in the html template: <div class="text" ng-if="titboton == 'B'" ng-cloak> Number to copy: {{number}} <button class="buttoncopy" ng-show="showbutton == 1" type="button" ng-click="copyText(number)"> <i class='icon ion-ios-copy'></i> </button> </div>

Martherius avatar Jan 20 '22 18:01 Martherius

@Martherius have you tried prefixing with window, like window.cordova.plugins.clipboard.copy(text);? Hopefully, I'll have my new machine setup and can test this out myself next week.

wuori avatar Jan 21 '22 01:01 wuori

@Martherius have you tried prefixing with window, like window.cordova.plugins.clipboard.copy(text);? Hopefully, I'll have my new machine setup and can test this out myself next week.

You mean instead of this:

$cordovaClipboard.copy(value)

I do this:

window.cordova.plugins.clipboard.copy(value)

ok thanks a lot this works for ios, although the notification does not show now

also this stopped working on android but i guess i just need to build android with option 1 and ios with option 2 Anyway thank you very much, been stuck with this for a while now.

also sorry to ask but i have one more issue with my app, i'm using com.darktalker.cordova.screenshot to take screenshots in the app and share them, works fine with iOS but for android works only on android 10 or less devices, android 11 and up it's not working anymore

Martherius avatar Jan 21 '22 17:01 Martherius

@Martherius I'm glad you made some progress! I believe the same code worked for me on both iOS and Android.

I've only used this plugin atop a Vue-based Cordova application. You'll just want to make sure the code can access the plugin within its current scope.

I'm not familiar with the screenshot plugin, you may want to follow up by raising an issue with them.

FYI for anyone else reading this here's an example I had working (a year ago, at least) to copy a remote image to a clipboard:

    copyImage(url){
        
        return new Promise((resolve, reject) => {

            let folderpath = this.getSavePath();
            let filename = "Treet-Photo.png";

            window.cordova.plugin.http.downloadFile(url,null,null,folderpath+filename,(fileEntry) => {

                fileEntry.file( (file) => {
                    
                    let reader = new FileReader();
            
                    reader.onloadend = function() {
                        // console.log("Successful file read: " + this.result);
                        window.cordova.plugins.clipboard.copy({
                            type: 'image',
                            data: this.result
                        }, (res) => {
                            resolve(res);
                        });
                    };
                    
                    reader.readAsDataURL(file);
            
                }, (e) => { reject(e); } );

            }, (e) => { reject(e); });

        });

    },

wuori avatar Jan 24 '22 01:01 wuori