2

这就是我所做的:

1)安装 Cordova 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问题是根本没有调用 来自的本机函数。

我在这里想念什么?

帮助将不胜感激。

4

2 回答 2

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

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

于 2012-10-08T11:41:00.857 回答
0

[1] 确保您在 index.html(您的入口 html 页面)中导入了“cordova.js”

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

=> 对于cordova.plist

核心价值

HelloPlugin - HelloPlugin

[或者]

=> 对于 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];

}

于 2016-08-31T09:30:14.327 回答