這是我做過什麼:無法從JavaScript的調用本地函數,在PhoneGap的
1)安裝科爾多瓦版本2.0.0
2)我的XCode版本4.3.3是
3)通過./create命令創建手機差距項目。
4)index.html
:
<script type="text/javascript">
function nativePluginResultHandler (result)
{
alert("SUCCESS: \r\n"+result);
}
function nativePluginErrorHandler (error)
{
alert("ERROR: \r\n"+error);
}
function callNativePlugin(returnSuccess)
{
alert("Invoking..");
HelloPlugin.callNativeFunction(nativePluginResultHandler, nativePluginErrorHandler, returnSuccess);
}
</script>
<h1>Hey, it's Cordova!</h1>
<button onclick="callNativePlugin('success');">Click to invoke the Native Plugin with an SUCCESS!</button>
<button onclick="callNativePlugin('error');">Click to invoke the Native Plugin with an ERROR!</button>
5)內部HelloPlugin.js
:
var HelloPlugin = {
callNativeFunction: function (success, fail, resultType) {
echo "Welcome";
return Cordova.exec(success, fail, "com.mycompany.HelloPlugin", "nativeFunction", [resultType]);
} };
6)HelloPlugin.m
:
#import "HelloPlugin.h"
@implementation HelloPlugin
- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
//get the callback id
NSString *callbackId = [arguments pop];
NSLog(@"Hello, this is a native function called from PhoneGap/Cordova!");
NSString *resultType = [arguments objectAtIndex:0];
CDVPluginResult *result;
if ([resultType isEqualToString:@"success"])
{
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @"Success :)"];
[self writeJavascript:[result toSuccessCallbackString:callbackId]];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"Error :("];
[self writeJavascript:[result toErrorCallbackString:callbackId]];
}
}
@end
7)HelloPlugin.h
:
#import "Cordova/CDVPlugin.h"
#import "Cordova/CDV.h"
@interface HelloPlugin : CDVPlugin
- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
8)在Cordova.plist
,我添加了以下的鍵/值:
com.mycompany.HelloPlugin HelloPlugin
的問題是,從HelloPlugin
本機函數是根本沒有被調用。
我在這裏錯過了什麼?
幫助將非常感激。
嗨!你嘗試加入'HelloPlugin'和'HelloPlugin',在Cordova.plist文件的插件部分,而不是'com.mycompany.HelloPlugin'和'HelloPlugin',? – Littm
Littm,我嘗試了,但仍然沒有被調用。 – Whoami
控制檯上是否有任何錯誤? ? – Littm