passing value from iOS native code to cordova

I have some values generated from my native code that I would like to pass to phonegap. These data are generated in real time and are not directly influenced by the user's actions through the phonegap gui.My native code is part of a plugin that I made.

What is the best way to approach this? I want to have a function to send data over anytime and have a listener on the cordova side. I'm using Cordova 1.5 with Xcode 4.3.

Here is what I have so far:

swipe.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: rn"+result );
        }

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

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

  </body> ...

swipe.h:

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

swipe.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]];
    }

...

The code that I have right now has nothing for doing what I want. I'm not really sure how to setup the code in both cordova and native.


Sounds like you need to be able to talk back down to PhoneGap from objective C, in which case you should be able to use something like:

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

And if you have a JS function like "hello" in your index.html like this:

function hello(){return "hello";}

Its a way of talking back to your PhoneGap web layer


create a class of type CDVPlugin

import #import in that class

initialize a handler method .h class

- (void)Device:(CDVInvokedUrlCommand *)command;

and implement the method in .m class

- (void)openDevice:(CDVInvokedUrlCommand *)command{
    CDVPluginResult *pluginResult = nil;
    BOOL checkOpenDevice=NO;
     pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:checkOpenDevice];
       [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
}

in this way through self.commandDelegate your data will be able to reach to the cordova class if the .js file hit(calls) that particular method which is initialized in .h class.

链接地址: http://www.djcxy.com/p/11048.html

上一篇: 磁性div:在绝对定位div内的固定div

下一篇: 将iOS本地代码的价值传递给cordova