如何在手机间隙应用程序中通过javascript在Native iOS自定义插件与Cordova 3.8 index.html之间进行通信?

时间:2015-06-01 10:11:19

标签: ios objective-c cordova cordova-plugins cordova-3.8.0

我已经引用了许多链接,用于使用

维护iOS自定义插件与cordova index.html文件之间的桥接
     -(void)methodName:(CDVInvokedUrlCommand*)command;

甚至提到:iOS JavaScript bridge

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

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

我的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

1 个答案:

答案 0 :(得分:4)

这解决了我从目标c类回调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 

   });


 }

现在我的输入回调方法正在执行