[英]Phonegap/Cordova 2.9 Custom Plugin creation. Any working examples?
我知道github上有大量的phonegap / cordova插件示例,但是我发现插件的构建方式之间存在许多不一致之处。 该结构看起来(大部分)是相同的,但是几乎每个代码和实现似乎都不同。 这让我问了几个问题。
2.9文档说要使用在配置中声明插件的方法,但是我会收到说要使用该方法的构建警告。 我应该同时使用吗?
在JavaScript中,声明/实例化插件的正确方法是什么?
我是通过window.MyPlugin.myMethod引用插件的方法还是仅仅是window.myMethod?
我还有其他问题,但是代码会很棒。
有没有人有一个绝对简单的示例示例,说明适用于iOS平台的cordova 2.9的自定义插件有效吗?
这是我几天前写的一个非常简单的插件,只是为了测试构建基于iOS的Cordova插件。
JS:
var tester = function() {};
tester.prototype.test = function () {
cordova.exec(
function(result) {
navigator.notification.alert('test plugin returned: '+result);
},
function() {
navigator.notification.alert('test plugin error');
},
'TestPlugin',
'test',
['Your test string']
);
};
if(!window.plugins) {
window.plugins = {};
}
if (!window.plugins.tester) {
window.plugins.tester = new tester();
}
致电:
<button onclick="window.plugins.tester.test()">TEST PLUGIN</button>
TestPlugin.h:
#import <Cordova/CDV.h>
@interface TestPlugin : CDVPlugin
- (void)test:(CDVInvokedUrlCommand*)command;
@end
TestPlugin.m:
#import "TestPlugin.h"
#import <Cordova/CDV.h>
@implementation TestPlugin
- (void)test:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* testString = [command.arguments objectAtIndex:0];
if (testString != nil && [testString length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:testString];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end
已添加到/platforms/ios/{PROJECT_NAME}/Resources/config.xml:
<plugin name="TestPlugin" value="TestPlugin" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.