无法从Phonegap中的javascript调用本机函数

时间:2012-10-08 07:09:38

标签: objective-c ios5 cordova ios4 phonegap-plugins

这就是我所做的:

1)已安装Cordova版本2.0.0

2)我的XCode版本是4.3.3

3)通过./create命令创建手机间隙项目。

index.html中的

4)

<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的本机函数根本没有被调用。

我在这里缺少什么?

非常感谢帮助。

2 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

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

HelloPlugin                      HelloPlugin

而不是:

com.mycompany.HelloPlugin        HelloPlugin

2 - 将您的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

您可以从上面的链接下载示例代码,这可能很有用:)。

答案 1 :(得分:0)

<强> [1] 确保您已在index.html(您的输入html页面)中导入了“cordova.js”

<强> [2] 您可以在 cordova.plist或config.xml中添加自定义插件

=&GT;对于cordova.plist

键 - 值

HelloPlugin - HelloPlugin

[OR]

=&GT;对于config.xml,添加以下功能标记

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

<强> [3] 然后在 HelloPlugin.m HelloPlugin.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];

}