【问题标题】:How do I write basic "Hello World" java script plugin in PhoneGap?如何在 PhoneGap 中编写基本的“Hello World”java 脚本插件?
【发布时间】:2013-01-16 21:56:07
【问题描述】:

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

编辑显示更新的代码:

让我给你看我的代码:

来自 config.xml:

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

现在是 Plugin.h:

#import <Cordova/CDV.h>

@interface MyPluginClass : CDVPlugin

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

@end

现在是 Plugin.m:

#import "Plugin.h"

@implementation MyPluginClass

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

@end

显示的第一个 html 页面称为“index.html”

我只想要一个空白的 html 页面,它只运行一个调用 cordova.exec() 函数的脚本。我这样做的尝试失败了。我不知道是我的脚本做错了什么还是我在其他地方做错了什么,但这是我的 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>

我收到以下错误日志:

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

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

【问题讨论】:

  • 这可能是一个愚蠢的问题,但这是你的整个 index.html 吗?您还需要加载cordova.js,并等待设备准备好触发...
  • 不,抱歉。我想我会简化一下我的帖子。我正在使用带有我添加的脚本的默认 Phonegap index.html 页面。我已经用完整的 html 更新了这个页面(减去顶部的评论):)

标签: javascript objective-c html cordova phonegap-plugins


【解决方案1】:

deviceready 事件触发之前,您不能对cordova 进行任何调用。做:

 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 应如下所示:

&lt;plugin name="MyPluginClass" value="MyPluginClass" /&gt;

(这些不一定必须相同,但name 应与 javascript 调用的第三个参数中的引用匹配,value 应与您的 Objective-C 类的名称匹配。

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

【讨论】:

  • 感谢您的回复以及您指向该文档的链接。但是,使用该站点上的 html,我收到以下错误:“错误:方法 ':'未在插件 'test' 中定义 -[CDVCommandQueue executePending] [第 103 行] FAILED pluginJSON = ["INVALID","test", "",[]]" 这里有什么问题?
  • 编辑了答案以提供更多细节,希望对您有所帮助!
  • 我觉得我们已经接近了,但我还没有完全到那里。感谢您在这里容忍我的无知。我已经更新了我的代码以更清晰,但我仍然遇到错误。我已经编辑了我的原始帖子以获取所有更新的代码。注意错误日志和我的 config.xml。它看起来是正确的,但我不确定出了什么问题。
  • 那行得通。但是我还有一个问题:如果我想为这个类添加多个方法,我将如何在 config.xml 中声明这些方法?每次我需要本地代码中的某些东西时,我真的必须要有一个新的插件类吗?还是我只保留 config.xml 并让 exec 更改其参数?
  • 插件中可以有多个方法——你不需要在 config.xml 中声明它们——你只需要在 config.xml 中映射到插件一次(对于类)。在您的 javascript 中,将 someMethod 更改为您的新方法,然后在您的插件类中添加新方法。
猜你喜欢
  • 2013-04-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2011-01-11
  • 2011-05-12
  • 2012-08-18
  • 2019-04-09
  • 1970-01-01
相关资源
最近更新 更多