2012-10-08 37 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]; 

}