Cordova插件无法从javascript界面调用本机ios方法。
我为iOS平台创建了一个简单的cordova插件,并将其添加到cordova项目中。但不知何故,我无法从插件的JS层调用本机代码。我已尝试在stackoverflow和cordova.apache.org网站上提供所有可能的解决方案。
下面是我的代码,请检查一下,如果我错过任何一步,请告诉我 -
的index.html
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="Merchant">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p> <br>
<button onclick="testInit()">Click</button>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/testXyz.js"></script>
</body>
index.js
var app = {
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
onDeviceReady: function() {
this.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
testXyz.js
function testInit() {
"use strict";
var sign = "xyz";
var environ = "S1";
var mp = cordova.require("cordova_Plugin_xyzPlugin.xyzPlugin");
var success = function() { alert('win!'); }
var fail = function() { alert('fail!'); }
mp.coolMethod(sign, environ, true, true, success, fail);
}
的plugin.xml
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova_Plugin_xyzPlugin" version="0.0.1"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<name>xyzPlugin</name>
<js-module name="xyzPlugin" src="www/xyzPlugin.js">
<clobbers target="cordova.plugins.xyzPlugin" />
</js-module><platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="xyzPlugin">
<param name="ios-package" value="xyzPlugin" />
</feature>
</config-file>
<source-file src="src/ios/xyzPlugin.h" />
<source-file src="src/ios/xyzPlugin.m" />
</platform>
</plugin>
xyzPlugin.js
cordova.define("cordova_Plugin_xyzPlugin.xyzPlugin", function(require, exports, module) {
var exec = require('cordova/exec');
var xyzPlugin = {};
xyzPlugin.coolMethod = function(arg0, arg1, arg2, arg3, success, error)
{
alert(JSON.stringify(cordova));
//Here I am getting Alert.
cordova.exec(success, error, "xyzPlugin", "coolMethod", [arg0, arg1, arg2, arg3]);
// This is not working, from here I am not able to call native method.
};
module.exports = xyzPlugin;
});
Native ios插件 -
xyzPlugin.h
#import <Cordova/CDV.h>
@interface xyzPlugin : CDVPlugin
- (void)coolMethod:(CDVInvokedUrlCommand*)command;
@end
xyxPlugin.m
#import "xyxPlugin.h"
@implementation xyxPlugin
-(void)coolMethod:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0];
if (echo != nil && [echo length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end
请帮助解决此问题。
提前致谢:)