這是一個非常簡單的插件,我寫了幾天去了,它只是測試構建一個基於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" />
首先,這是一個很好的回覆。我非常感謝簡潔而全面的代碼。謝謝。我遇到了同樣的問題,我一直在摔跤幾天。當我點擊該測試按鈕時,沒有任何反應。也許我不是在正確的地方尋找錯誤。儘管如此,控制檯中似乎沒有任何東西出現。有任何想法嗎? – QuailAndQuasar