objective c - 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 maintaining bridge between ios custom plugin cordova index.html file using
-(void)methodname:(cdvinvokedurlcommand*)command;
and referred : ios javascript bridge
but want maintain direct connection myplugin index.html.can suggest me better way implement this.
i have created myplugin.js , myplugin.h , myplugin.m classes update location every 10sec. want send these (latitude , longitude parameters)from myplugin.m(ios plugin class) index.html class 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 "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 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
this solved issue callback javascript 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 call requires javascript method objective c class /* here echo 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 required method }); }
now callback method input executing perfectly
Comments
Post a Comment