2012-10-08 15 views
2

这是我做过什么:无法从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本机函数是根本没有被调用。

我在这里错过了什么?

帮助将非常感激。

+0

嗨!你尝试加入'HelloPlugin'和'HelloPlugin',在Cordova.plist文件的插件部分,而不是'com.mycompany.HelloPlugin'和'HelloPlugin',? – Littm

+0

Littm,我尝试了,但仍然没有被调用。 – Whoami

+0

控制台上是否有任何错误? ? – Littm

回答

1

你可以尝试以下方法:

1 -在您的文件Cordova.plist,下面的键/值添加到插件部分:

HelloPlugin      HelloPlugin 

代替:

com.mycompany.HelloPlugin  HelloPlugin 

2 - Ch安格你的JavaScript文件HelloPlugin.js的内容如下:

var HelloPlugin = { 
    callNativeFunction: function (success, fail, resultType) { 
     console.log ("Welcome"); 
     return Cordova.exec(success, fail, "HelloPlugin", "nativeFunction", [resultType]); 
    } }; 

3 -您的HTML文件index.html更改为以下:

<html> 
    <header> 

     <script type="text/javascript" src="./js/HelloPlugin.js"></script> 

     <script type="text/javascript"> 

      document.addEventListener("deviceready",onDeviceReady,false); 

      function onDeviceReady() 
      { 
       // do your thing! 
       alert("Cordova is working") 
      } 

      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> 
    </header> 

    <body> 

     <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> 

    </body> 
</html> 

希望这有助于。让我知道这个是否奏效。

另外,我发现这个链接,我想你会发现很有趣:

http://www.adobe.com/devnet/html5/articles/extending-phonegap-with-native-plugins-for-ios.html

有一个示例代码,你可以从上面的链接,这可能是有用的:)下载。

+0

嘿Littm,谢谢。帮助,它的愚蠢,我没有正确更新javascript路径,它现在可以工作。 – Whoami

0

[1] 确保您已在进口的index.html “cordova.js”(您的输入HTML页面)

[2] 您可以通过添加自定义的插件在cordova.plist OR config.xml中

=>对于cordova.plist

键 - 值

与HelloPlugin - 与HelloPlugin

[OR]

=>有关config.xml中,添加如下特征标志

<feature name="HelloPlugin"> 
     <param name="ios-package" value="HelloPlugin" /> 
     <param name="onload" value="true" /> 
</feature> 

[3] 然后,让你的下面的变化功能HelloPlugin.mHelloPlugin.h

-(void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { 

要,

- (void) nativeFunction:(CDVInvokedUrlCommand*)command { 

CDVPluginResult* pluginResult = nil; 
NSString* echo = [command.arguments objectAtIndex:0]; 

if (echo != nil && [echo length] > 0) { 
    //pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo]; 
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Sandip"]; 
} else { 
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; 
} 


if ([echo isEqualToString:@"success"]) 
{ 
    NSLog(@"Native log : success"); 
} else { 
    NSLog(@"Native log : failed"); 
} 

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 

}