7

phonegap に渡したいネイティブ コードから生成された値がいくつかあります。これらのデータはリアルタイムで生成され、phonegap gui を介したユーザーのアクションに直接影響されることはありません。私のネイティブ コードは、私が作成したプラグインの一部です。

これにアプローチする最良の方法は何ですか?いつでもデータを送信できる機能を持ち、コルドバ側にリスナーを持ちたいです。Xcode 4.3 で Cordova 1.5 を使用しています。

これが私がこれまでに持っているものです:

スワイプ.js:

var swipe={
     callNativeFunction: function (success, fail, resultType) {
    return Cordova.exec( success, fail, 
                        "ca.swipe", 
                        "nativeFunction", 
                        [resultType]); }

};

index.html:

...

function callNativePlugin( returnSuccess ) {
            swipe.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
        }

        function nativePluginResultHandler (result) {
            alert("SUCCESS: \r\n"+result );
        }

        function nativePluginErrorHandler (error) {
            alert("ERROR: \r\n"+error );
        } ...  <body onload="onBodyLoad()">     <h1>Hey, it's Cordova!</h1>

      <button onclick="callNativePlugin('success');">Success</button>
      <button onclick="callNativePlugin('error');">Fail</button>

  </body> ...

スワイプ.h:

...
@interface swipe : CDVPlugin
- (void) nativeFunction:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end

スワイプ.m:

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

    NSLog(@"Hello, this is a native function called from PhoneGap/Cordova!");

    //get the callback id
    NSString *callbackId = [arguments pop];
    NSString *resultType = [arguments objectAtIndex:0];     
    NSMutableArray *GlobalArg=arguments;

    CDVPluginResult *result;
    if ( [resultType isEqualToString:@"success"] ) {
       result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @"Success :)"];
       //writes back the smiley face to phone gap. 
       [self writeJavascript:[result toSuccessCallbackString:callbackId]];
    }

...

私が今持っているコードには、私がやりたいことをするためのものは何もありません. コードバとネイティブの両方でコードをセットアップする方法がよくわかりません。

4

2 に答える 2

2

目的 C から PhoneGap に戻ることができるようにする必要があるように思えます。その場合、次のようなものを使用できるはずです。

 NSString *jsResult = [theWebView stringByEvaluatingJavaScriptFromString:@"hello()"];
 NSLog(@"jsResult=%@",jsResult);

index.html に "hello" のような JS 関数がある場合は、次のようになります。

function hello(){return "hello";}

これは、PhoneGap Web レイヤーに話しかける方法です

于 2012-09-13T12:06:35.080 に答える