如何写基本的“Hello World” java脚本插件在PhoneGap? [英] How do I write basic "Hello World" java script plugin in PhoneGap?

查看:217
本文介绍了如何写基本的“Hello World” java脚本插件在PhoneGap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读Cordova的教程,但我不确定他们是否给我足够的信息。



已编辑以显示更新的代码:



让我告诉你我的代码:



从config.xml:

 < plugin name =someMethodvalue =MyPluginClass/> 

现在对于Plugin.h:

  #import< Cordova / CDV.h> 

@interface MyPluginClass:CDVPlugin

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

@end

现在Plugin.m:

  #importPlugin.h

@implementation MyPluginClass

- (void) someMethod:(CDVInvokedUrlCommand *)command
{
NSLog(@您正在从PLUGIN读取此文件);
}

@end

被显示为index.html



我只想要一个空白的html页面,它只是运行一个调用cordova.exec()函数的脚本。我这样做的尝试失败了。我不知道是否有一些我做错了我的脚本或我做错了别的地方,但这里是我的index.html:

 <!DOCTYPE html> 
< html>
< head>
< title> Cordova设备就绪示例< / title>

< script type =text / javascriptcharset =utf-8src =cordova-2.3.0.js>< / script&
< script type =text / javascriptcharset =utf-8>

//加载Cordova时调用onDeviceReady。
//
//此时,文档已加载,但cordova-2.3.0.js没有加载。
//当Cordova加载并与本机设备通信时,
//它将调用事件`deviceready`。
//
function onLoad(){
document.addEventListener(deviceready,onDeviceReady,false);
}

//加载Cordova并且现在可以安全地调用Cordova方法
//
function onDeviceReady(){
// Now安全使用Cordova API
document.addEventListener(deviceready,function(){
cordova.exec(null,null,MyPluginClass,someMethod,[]);
},false);
}
< / script>
< / head>
< body onload =onLoad()>
< / body>
< / html>

我得到以下错误日志:



2013-01-17 11:36:31.782 CCT [1293:907]错误:找不到插件MyPluginClass,或者不是CDVPlugin。在config.xml中检查插件映射。



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

解决方案

to cordova,直到 deviceready 事件触发。 Do:

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



编辑



对于上面列出的示例调用,您需要一个类似于以下内容的Objective-C类:

  @interface MyPluginClass :CDVPlugin 

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

@end

请注意类的名称,方法,它匹配 cordova.exec



另一个编辑:



您的 config.xml 应如下所示:



< plugin name =MyPluginClassvalue =MyPluginClass/>



必须是相同的,但 name 应该匹配JavaScript调用的第三个参数中的引用, value 应该与您的Objective-C类的名称匹配。



有关开发iOS插件的完整文档,请查看指南


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",[]]

解决方案

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

这篇关于如何写基本的“Hello World” java脚本插件在PhoneGap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆