9

i am developing an app using javascript/mobile-jquery interface for the phonegap platform. Now I have seen so many examples on the web trying to copy to clipboard and unfortunatelly none of them worked for me. I am not interested in this being function-able in the browser, as long as it works once it is converted by phone gap I am happy.

I have tried using zeroclipboard, it did not workout, I have tried using clipboard manager could not get it to work. I have tried many other examples that I found here on stackoverflow including google search and they still did not work, here is an example of things i've tried:

window.plugins.clipboardManager.copy(
                "the text to copy",
                function(r){alert("copy is successful")},
                function(e){alert(e)}
            );

I have included the js file:

    <script src="src/clipboardmanager.js"></script>

and I also have the java file in the folder structure as this: src\com\saatcioglu\phonegap\clipboardmanager\ClipboardManagerPlugin.java

From what I've read I need to include an xml file for this to work, but for the life of me I could not find that XML file anywhere.

Any help is most appreciated.

Note: My app will require no permissions such as camera, gps, etc...

EDIT:

Another example I tried was:

function select_all(obj) {
    var text_val=eval(obj);
    text_val.focus();
    text_val.select();
    if (!document.all) return; // IE only
    r = text_val.createTextRange();
    r.execCommand('copy');
}

This worked in IE but not in Phonegap.

EDIT:

Here is the html/javascript I'm using:

<html>
    <head>
        <title>Test</title>
            <link rel="stylesheet" href="jquery/jquery.mobile-1.3.1.min.css" />
        <script src="jquery/jquery-1.9.1.min.js"></script>
        <script src="jquery/jquery.mobile-1.3.1.min.js"></script>
            <script src="clipboardmanager.js"></script>
        <script>
                var cbm = new window.plugins.clipboardManager;
                function main(textMessage)
                {
            //Some Code before this (calculations)
                    cbm.copy(
                        "Success!!!",
                        function(r){alert("copy is successful")},
                        function(e){alert(e)}
                    );
                }
            </script>
        </head>
        <body>
        <div data-role="page" id="main" name="main">
            <div data-role="header">
                <h1>Test</h1>
                </div><!-- /header -->

            <div data-role="content">
                <form action="javascript:main(encryptedMessage.value);">
                    Message to be Copied:
                    <textarea id="encryptedMessage" name="encryptedName" rows="6" style="width:99%;"></textarea>
                    <input type="submit" value="Encrypt" />
                </form>
                </div>
        </div>
    </body>
</html>

In my root folder I have:

  1. a folder called jquery which has jquery scripts in there.
  2. a folder called res which has a folder called xml which has a file called plugin.xml
  3. a folder called src which has a folder called com, which has a folder called saatcioglu, which has a folder called phonegap, which has a folder called clipboardmanager, which has a file called ClipboardManagerPlugin.java.
  4. test.html
  5. clipboardmanager.js

Contents of plugin.xml

<?xml version="1.0" encoding="utf-8"?>
<plugins>
    <gap:plugin name="clipboardmanager" value="com.saatcioglu.phonegap.clipboardmanager.ClipboardManagerPlugin.ClipboardManagerPlugin" />
</plugins>

What have I done wrong?

Bagzli
  • 6,254
  • 17
  • 80
  • 163

2 Answers2

6

First up: that IE option will not work on Android as PhoneGap uses Webkit (think: Safari and/or Chrome).

Anyway...

That file you're looking for (in the "/res/xml/" subdirectory of your project's directory) is called

config.xml

In there, you have to tell phonegap to load the plugin at compile time like this...

<gap:plugin name="whatever" value="com.example.whatever" />

If you don't do that, phonegap will simply not include the plugin at compile time, resulting in the fact that your plugin won't work (since it doesn't exist in the compiled apk).

I haven't used the ClipboardManagerPlugin yet, but according to the docs it should go somewhat like this:

<gap:plugin name="clipboardmanager" value="com.saatcioglu.phonegap.clipboardmanager.ClipboardManagerPlugin.ClipboardManagerPlugin" />

Please note that you should check the PhoneGap version you're using and if the plugin is compatible with it. Just in case you're not aware of it: not all plugins have been updated to work with PhoneGap 3.x yet. To quote the readme at Github (https://build.phonegap.com/docs/plugins-using): "Unless explicitly stated, most of these plugins will not work with Cordova/PhoneGap 3.x.x out of the box. They will need updating before they can be used via the plugin add interface."

e-sushi
  • 13,786
  • 10
  • 38
  • 57
  • Thank you for the reply, so from what you say this should also work in the chrome browser then, correct? I am trying to get it to work in the browser first then but with no luck. I am editing my question now to show exactly what I am doing, could you please go over it and let me know of my mistake? – Bagzli Aug 26 '13 at 13:52
  • @Bagzli Erm, it's probably a speed-error of yours, but you're not loading the phonegap.js in your EDIT like this: *""*. Also, you can not test phonegap functionality which targets devices in your regular browser. You will need either a device in dev-mode, or you will need to use the Android SDK emulators. – e-sushi Aug 26 '13 at 14:54
  • well, i tried deploying it in phonegap and ran into a problem, i cannot have src folders for blackberry, so it would not compile that. All i have is a blackberry phone to test this on, so I don't even know if this works or not. Do you happen to know any other solution I can use to copy to clipboard? – Bagzli Aug 26 '13 at 14:56
  • @Bagzli Well, how do you want to code an app for blackberry if you haven't installed the related SDK files? That won't work at all! Before wasting time, you might want to check you actually installed the SDK completely, with all files you need to target your device's system. (A note aside: when installing the SDK on Windows operating systems, the SDK installs the needed Blackberry files natively. It's Android and iPhone that will be harder to configure for development on Windows.) Re to your *"other solution"* request: besides native app development and phonegap, I don't see how to code this. – e-sushi Aug 26 '13 at 15:04
  • @Bagzli The reason why I can't imagine other solutions to exists: how do you want to access your blackberry's clipboard if you have no programatically way to access it? After all, that's the reason why you code an app (phonegap or native) - to programatically be able to interact with the device's system. As said: your current status-quo seems to be that you haven't (completely) installed PhoneGap yet. You'll need to cope with that before being able to solve related software questions, because you need to be able to compile an app for your target device. If you can't, you can't go develop... ;) – e-sushi Aug 26 '13 at 15:06
  • @Bagzli To get you going, make sure you followed the install procedure at http://phonegap.com/install/. Then get the dev-environment (like ADT, which is "Eclipse + Android SDK plugin"). When done, you are able to create and compile the "Hello World" example contained in Phonegap. This first app will help you because it can provide a working base for your own app, and it also shows your environment is then ready for (blackberry) app development! Good luck… and in case of doubt, or if you run into any specific install problems, simply create another question at StackOverflow for us to answer. ;) – e-sushi Aug 26 '13 at 15:15
  • I have one question regarding what you said, will i still be able to run that code on multiple platforms if I do it your way? I've stuck to html/javascript so that I can target all platforms, I don't want to lose that. – Bagzli Aug 26 '13 at 17:50
  • 1
    @Bagzli Sure! That's why you would use phonegap in the first place. Phonegap provides a "wrapper" that enables you to code HTML/CSS/JS and use device-native functionality (like GPS, vibration, etc.) via phonegap.js. When your phonegap app works on blackberry and you want to compile that same app to work on android, you only have to target android during compile... but you don't have to change a bit of your app's HTML/CSS/JS code. Think of phonegap as a bridge between your HTML/CSS/JS and native device functionality of (supported) devices. Same code... target other device during compile. Done. – e-sushi Aug 26 '13 at 17:57
5

e-sushi's instructions didn’t quite work for me.

To get the plugin running I used the following command:

phonegap local plugin add https://github.com/VersoSolutions/CordovaClipboard

and then adding the following command in JS:

cordova.plugins.clipboard.copy(text);
Community
  • 1
  • 1
Marcom
  • 4,621
  • 8
  • 54
  • 78