Как установить связь между пользовательским плагином Native iOS с помощью Cordova 3.8 index.html с помощью javascript в приложении для разрыва телефона?
Я сослался на множество ссылок для поддержания моста между пользовательским плагином iOS с помощью файла cordova index.html, используя
-(void)methodName:(CDVInvokedUrlCommand*)command;
И даже указано: iOS JavaScript мост
Но я хочу поддерживать прямое соединение 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 ответ
Решение
Это решило мою проблему с обратным вызовом JavaScript из класса C:
- (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
});
}
Теперь мой метод обратного вызова с вводом выполняется отлично