Search code examples
iosobjective-ccordovacordova-pluginscordova-3.8.0

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


I have referred many links for maintaining bridge between iOS custom plugin with cordova index.html file using

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

And even referred : iOS JavaScript bridge

But I want to maintain a direct connection from myplugin to index.html.Can anyone suggest me the better way to implement this.

I have created myplugin.js and MyPlugin.h and MyPlugin.m classes to update location for every 10Sec. Now I want to send these (latitude and longitude parameters)from myplugin.m(iOS plugin class) to index.html class as arguments

My 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

Solution

  • This solved my issue to callback javascript from objective c class:

       - (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 
    
       });
    
    
     }
    

    Now my callback method with input is executing perfectly