这是我几天前写的一个非常简单的插件,它只是为了测试构建一个基于 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>
测试插件.h:
#import <Cordova/CDV.h>
@interface TestPlugin : CDVPlugin
- (void)test:(CDVInvokedUrlCommand*)command;
@end
测试插件.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" />