如何在 phone 差距应用程序中通过 javascript 在原生 iOS 自定义插件与 Cordova 3.8 index.html 之间进行通信?

how to communicate between Native iOS custom plugin with Cordova 3.8 index.html via javascript in a phone gap application?

我已经提到了许多链接,用于维护 iOS 自定义插件与 cordova index.html 文件之间的桥梁,使用

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

甚至提到:iOS JavaScript bridge

但我想保持从 myplugin 到 index.html 的直接连接。任何人都可以建议我更好的实现方法。

我创建了 myplugin.js 和 MyPlugin.h 以及 MyPlugin.m classes 以每 10 秒更新一次位置。现在我想将这些(纬度和经度参数)从 myplugin.m(iOS 插件 class)发送到 index.html class 作为参数

我的plugin.js

           //myButton1
           function MyPlugin() {}

           MyPlugin.prototype.sayHelloCustom = function(data,data2) {

           exec(function(result){
                alert('succescallback :' + result);}, //1.success callbal
                function(error){alert("Error" + error);   }, // 2.error call back
                "MyPlugin",                               //3.Native plugin calss name
                "sayHelloCustom",                        //4.Method name in Myplugin.m

                [{
                 "RequestId":data,
                 "ServiceName":data2   //5. optional argurments array

                }]        

                );
           }

           var myPlugin = new MyPlugin();
           module.exports = myPlugin
           });

MyPlugin.m

- (void)sayHelloCustom:(CDVInvokedUrlCommand*)command
   {
      if(!isUpdatingLocation == YES){
          [self startUpdatingLocation];
        }


if ([CLLocationManager locationServicesEnabled]) {
    // Find the current location
    [self->locationManager startMonitoringSignificantLocationChanges];
    //rest of code...
}

     bgTask =0;
     app = [UIApplication sharedApplication];
     bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
}];
//110
timer = [NSTimer
         scheduledTimerWithTimeInterval:10.0
         target:self
         selector:@selector(timerCountDown:)
         userInfo:nil
         repeats:YES];

 Str =[NSString stringWithFormat:@"%@",[NSDate date]];
 NSString *responseString = [NSString stringWithFormat:@"Hello %@", [command.arguments objectAtIndex:0]];

 CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:responseString];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}

   -(void)timerCountDown:(CDVInvokedUrlCommand*)command{

      [self->locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
      [self->locationManager setDistanceFilter:kCLDistanceFilterNone];
   }//to update location

这解决了我从 objective c class:

回调 javascript 的问题
   - (void) sayHelloCustom:(CDVInvokedUrlCommand*)command
   {

    NSString *methodname;
    NSString * requestIdStr;
    NSDictionary* options = [[NSDictionary alloc]init];

if ([command.arguments count] > 0) {
    options = [command argumentAtIndex:0];
    requestIdStr = [options objectForKey:@"requestId"];
    methodname =[options objectForKey:@"callback"];

}


dispatch_async(dispatch_get_main_queue(), ^{
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"12"];
    [pluginResult setKeepCallbackAsBool:true];

     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 

    //This helps to call back the requires javascript method from objective c class 
  /* Here echo is jsonstring :  {"latitude":"78.431091",
                                  "network":"true",
                                  "response":"100",
                                  "requestId":"trackMe-262",
                                  "longitude":"17.462852"}*/

     NSString * jsCallBack = [NSString stringWithFormat:@"%@(%@);", methodname, echo];//methodname(argument)

         [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];//this calls back required method 

   });


 }

现在我的带输入的回调方法执行得很好