How do I write basic “Hello World” java script plugin in PhoneGap?

痴心易碎 提交于 2019-12-10 09:30:54

问题


I've read Cordova's tutorials, but I'm not sure they've given me enough information.

EDITED TO SHOW UPDATED CODE:

Let me show you my code:

From the config.xml:

<plugin name="someMethod" value="MyPluginClass" />

Now for the Plugin.h:

#import <Cordova/CDV.h>

@interface MyPluginClass : CDVPlugin

- (void)someMethod:(CDVInvokedUrlCommand*)command;

@end

Now for the Plugin.m:

#import "Plugin.h"

@implementation MyPluginClass

- (void)someMethod:(CDVInvokedUrlCommand *)command
{
    NSLog(@"YOU ARE READING THIS NATIVELY FROM A PLUGIN");
}

@end

The very first html page that gets displayed is called "index.html"

I just want a blank html page which simply runs a script that calls the cordova.exec() function. My attempts in doing so have failed. I don't know whether there's something I've done wrong with my script or something I've done wrong elsewhere but here's my index.html:

<!DOCTYPE html>
<html>
<head>
    <title>Cordova Device Ready Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
    <script type="text/javascript" charset="utf-8">

        // Call onDeviceReady when Cordova is loaded.
        //
        // At this point, the document has loaded but cordova-2.3.0.js has not.
        // When Cordova is loaded and talking with the native device,
        // it will call the event `deviceready`.
        //
        function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }

        // Cordova is loaded and it is now safe to make calls Cordova methods
        //
        function onDeviceReady() {
            // Now safe to use the Cordova API
            document.addEventListener("deviceready", function() {
                                      cordova.exec(null,null,"MyPluginClass","someMethod",[]);
                                      }, false);
        }
        </script>
    </head>
    <body onload="onLoad()">
    </body>
</html>

I get the following error logs:

2013-01-17 11:36:31.782 CCT[1293:907] ERROR: Plugin 'MyPluginClass' not found, or is not a CDVPlugin. Check your plugin mapping in config.xml.

2013-01-17 11:36:31.787 CCT[1293:907] -[CDVCommandQueue executePending] [Line 103] FAILED pluginJSON = ["INVALID","MyPluginClass","someMethod",[]]


回答1:


You can't make any calls to cordova until the deviceready event fires. Do:

 document.addEventListener("deviceready", function() {
    cordova.exec(null,null,"MyPluginClass","someMethod",[]);
 }, false);

Edit:

For the example call listed above, you'd need an Objective-C class that looks like:

@interface MyPluginClass : CDVPlugin

- (void)someMethod:(CDVInvokedUrlCommand*)command;

@end

Note the name of the class, and name of the method, which match the call to cordova.exec

Another Edit:

Your config.xml should look like the following:

<plugin name="MyPluginClass" value="MyPluginClass" />

(these don't necessarily have to be the same, but name should match the reference in the third argument of the javascript call, and value should match the name of your Objective-C class.

For full documentation on developing a plugin for iOS, check out the guide



来源:https://stackoverflow.com/questions/14368597/how-do-i-write-basic-hello-world-java-script-plugin-in-phonegap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!