Ionic-native: Clipboard copy/paste not working

Created on 3 Jul 2017  Â·  21Comments  Â·  Source: ionic-team/ionic-native

_From @thanhdn9120 on July 2, 2017 14:55_

Hi everybody,
I'm using ionic 3, I want to implement copy/paste function but currently it not working
below is my code (It's very simple):

// copy function
   this.clipboard.copy('Hello world').then(rs => {
      alert(rs);
    }).catch(error => {
      alert(error);
    })
// paste function
    this.clipboard.paste().then(
      (resolve: string) => {
          alert(resolve);
        },
        (reject: string) => {
          alert('Error: ' + reject);
        }
      );
    }

when I running on:

  • Android: nothing happen
  • IOS: It's alert error "plugin is not install" although I installed plugin Clipboard before

Anybody can help me. I think it very simple but It's amazing that this function is not working.
Thanks,
Thanh Nguyen

_Copied from original issue: ionic-team/ionic#12231_

waiting for user reply

Most helpful comment

Remove the Clipboard plugin you already have, and install this one:

cordova plugin add cordova-clipboard

It's a package that I published (forked from original plugin) and I am currently using it in my Ionic Native Playground app without issues.

If it works well, let me know and I will change the docs.

All 21 comments

_From @AmitMY on July 2, 2017 15:18_

  • Please fill the issue template
  • First thing I would check is remove platforms and plugins folders, and ionic cordova prepare, then make sure that the plugin is in the plugins folder, if it not, reinstall it.
  • If that doesn't work, please create an issue with ionic-native as clipboard is a native plugin and is not related to the ionic core

I have also encountered this problem
Is there still a solution?

Same here

Same here

Remove the Clipboard plugin you already have, and install this one:

cordova plugin add cordova-clipboard

It's a package that I published (forked from original plugin) and I am currently using it in my Ionic Native Playground app without issues.

If it works well, let me know and I will change the docs.

@ihadeed, thank you! It works!

Hot! Hot! Hot! Yewwwww!
Red Hot!!!!!!!!!!!!!!!!!!
Thank You from California!!!!!!!!!!!!!!!!!!!!!!!!!!!!
You are so awesome!!!!!!!!!!!!!!!!!!!!!!!!!!!!
You keep it going!!!!!!!!!!!!!!!!!!!
Ripping it up!!!!!!!!!!!!!!!!!!!!!

// IONIC 3.5.0 ON ANDROID 7.0 FROM FEDORA 25 ON MAC 27

//REMOVE OLD
$ ionic cordova plugin remove cordova-clipboard

// ADD NEW
$ cordova plugin add cordova-clipboard --save

// RESTART
$ ionic cordova run android --livereload --console

// ADD TO CONTROLLER
//...
import { Platform } from 'ionic-angular';
//...
declare var cordova: any;
//...
export class HomePage {
//...
constructor(
public platform: Platform
) {
}
//...
ionViewDidLoad() {
this.platform.ready().then(()=>{
cordova.plugins.clipboard.copy("Who Loves To
Code!!!!!!!!!!!!!!!");
cordova.plugins.clipboard.paste(function (text) { alert(text);
});
});
}
//...

On Tue, Jul 11, 2017 at 11:35 AM Ibby Hadeed notifications@github.com
wrote:

Closed #1758 https://github.com/ionic-team/ionic-native/issues/1758.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/ionic-team/ionic-native/issues/1758#event-1159361411,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAMYjrZj8SVlyqdcPjPvr9RFP_ZTzypPks5sM8BugaJpZM4OMXly
.

@ihadeed thank you for your comment.
Works like a charm

Not working for me on Android. Not sure what I've done differently. Seems to work fine on iOS.

Cordova 7.0.0
Cordova Android 6.2.3
ionic-angular "^3.6.0"
ionic-native/clipboard "4.1.0"
cordova-clipboard "^1.0.0"

No errors in logs.

About this plugin -- Say I use this to copy some text by pressing a button in my app, should I be able to open up another app and use the regular paste in ios (hold finger down) to paste that text, into say an email?

@sheamusburns did you manage to do it?

No - I still haven't got it to work properly. Looking for ideas.

On Thu, Oct 26, 2017, 2:41 AM jdmadriz notifications@github.com wrote:

@sheamusburns https://github.com/sheamusburns did you manage to do it?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/ionic-team/ionic-native/issues/1758#issuecomment-339567164,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AIEgSGPPH0qfVdcL1DAtrDeurBB5IU4Sks5swCmogaJpZM4OMXly
.

>

Sheamus Burns
@sheamusburns
http://sheamus.io

I still haven't gotten this to work on Android, either.

@ihadeed ping, just in case this isn't showing up because it's closed.

My experience is the opposite. It works on android perfectly, but not on ios. This is my code.

clipboard.copy('12345678');
clipboard.paste().then(
(resolve: string) => {
alert(resolve);
},
(reject: string) => {
alert('Error: ' + reject);
}
);

When I look at what has been copied to clipboard, it is not the string "12345678" but rather an 8 bytes text file.

Hi guys,

I'm try to use this plugin, but isn't work for me.

I'm trying to use in browser, run the server by "ionic serve".

My code was based on the @davemake sample.

import { Component } from '@angular/core';
import { NavController, NavParams, ToastController, ModalController, LoadingController,AlertController,Platform} from 'ionic-angular';
[...]
declare var cordova: any;
[...]
export class ExtratoRevendedorPage {
[...]
    constructor(
        [...]
        public platform: Platform,
        public loadingCtrl: LoadingController) {[...]}

        [...]
    abrirNovo(lancamento){
        [...]
        if(lancamento.FormaPgto == 'CARTAO'){
            let link = "https://checkcred.com.br/cliente/adesao/"+lancamento.idFatura;
            cordova.plugins.clipboard.copy("Who Loves To Code!!!!!!!!!!!!!!!");
            [...]
        }
    }
}

Someone can help me to make right this code?

Triggered Error: Cordova is not defined

@lucasamparo

Because this is a native plugin, it's only going to work when you run it on the device itself using a native install I think. I do not think any of the plugins run properly in the browser-based testing. The native plugins are meant to access the native apis of the device and so you need the actual device for it to run in most (if not all I think) cases. Once you get the code right, try doing a build and loading it onto your test device. (I could totally be off with that, but it's been my experience).

So there are a couple reasons you're running into problems -- you're declaring cordova globally, but it looks like cordova isn't available to you from inside this file using the declare keyword -- that's mainly because this plugin isn't supposed to be accessed like that. To get access to it, and make sure you run the ionic cordova plugin add clipboard command on it so Ionic loads into the package.json, then see if the code below helps.

in app.module.ts:
import { Clipboard } from '@ionic-native/clipboard';
and add Clipboard to the providers: array

I ended up using it by importing the plugin into the page controller class I was building in your case that would be ExtratoRevendedorPage

import { Clipboard } from '@ionic-native/clipboard';

then injecting it into the constructor of the class.

constructor( public clipboard: Clipboard, ...){}

Later I would call it using:

this.clipboard.copy('whatever string you want')

I never used this.clipboard.paste inside my app, but found that the device did copy the content I passed into .copy() into the device clipboard and I could paste into other apps.

Hope you get it working.

@shanghen , the ClipboardJS work on browser, after a workaround solution hahaha Thanks.
@sheamusburns , don't have time to read and analise all your comment yet. After I read then, I say something. But, thanks anyway.

I managed to make the plugin work running on --prod

declare var cordova; // on top of file

// on some method 
cordova.plugins.clipboard.copy('YAY IT WORKS', alert('COPIED'));

I have a similar problem:

my app use paste to copy a text as body for mail and/or whatsupp application.
it works well the first time, but i I switch application to copy something, the paste mwthod stop working until I clear the clipboard. I tried to use also the plugin mentioned by @ihadeed,

here a part of code:

 cordova.plugins.clipboard.paste(
      (resolve: string) => {
        console.log(resolve);
        window.open("mailto:" + (email || '') + "?body=" + encodeURIComponent(resolve), "_system");
      }, (reject: string) => {
        console.log(reject);
        window.open("mailto:" + (email || ''), "_system");
      }
    )

note that I tried to use also a var called this.clipboiard initalized in the constructor without success.

any idea in how to solve my problem?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sabariferin picture sabariferin  Â·  4Comments

vaibsVB picture vaibsVB  Â·  4Comments

hobbydevs picture hobbydevs  Â·  3Comments

mateo666 picture mateo666  Â·  3Comments

ajcrites picture ajcrites  Â·  3Comments