Я работаю над плагином Ionic для iOS и хочу отправить обратный вызов в файл component.ts приложения из пользовательского файла Objective-C.
Вот мой пример кода:
MyCustomPlugin.js
var exec = require('cordova/exec');
exports.showConsole = function (arg0, success, error) {
exec(success, error, 'MyCustomPlugin', 'MyCustomPluginMethod1', [arg0]);
};
exports.myCustomCallbackMethodWithData= function (success, error) {
exec(
success, // success callback function
error, // error callback function
'MyCustomPlugin', // mapped to our native Java class called
'myCustomCallbackMethodWithData', // with this action name
[{}]);
};
MyCustomPlugin.m
-(void)myCustomCallbackMethodWithData:(CDVInvokedUrlCommand *)command {
id observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"myCustomCallback" object:nil queue:nil usingBlock:^(NSNotification *notification) {
CDVPluginResult* pluginResult = nil;
NSLog(@"callback data = %@",[notification userInfo]);
NSData *userInfoData = [NSJSONSerialization dataWithJSONObject:[notification userInfo] options:0 error:NULL];
NSString *userInfo = [[NSString alloc] initWithData:userInfoData encoding: NSUTF8StringEncoding];
pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary:[notification userInfo]];
[self.commandDelegate evalJs:[NSString stringWithFormat:@"app.components.myComponentJSFunction(%@)",userInfo]];
}];
}
MYCustomiOSFile2.m
-(void)myCustomMethod {
NSDictonary *myuserInfo = @{@"MyKey1" : @"MyValue1"};
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"myCustomCallback" object:self userInfo: myuserInfo];
}
Когда MYCustomiOSFile2 выполняется внутренне из библиотеки iOS, пользовательское уведомление срабатывает на "myCustomCallback" и отправляет данные в MyCustomPlugin файл, но теперь я хочуполучить эти данные в моем ионном файле app.component.ts , как я могу это сделать?Все остальные методы MyCustomPlugin.js работают на app.component.ts обратный вызов только для файла не получен.
Ниже приведен код MyApp Component.ts
app.component.ts
import { Component } from '@angular/core';
import { Platform, App } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { TabsPage } from '../pages/tabs/tabs';
declare var MyCustomPlugin: any;
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = TabsPage;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
MyCustomPlugin.MyCustomPluginMethod1(); // This is working fine for me.
MyCustomPlugin.myCustomCallbackMethodWithData( function (data) {
alert("success"+JSON.stringify(data));
}, function () {
alert("error");
}
);
});
function myComponentJSFunction( function (data) {
alert("success"+JSON.stringify(data));
}, function () {
alert("error");
}
);
});
}
Здесь я хочучтобы получить мои опубликованные данные (то есть {"MyKey1": "MyValue1"}) в моем файле app.component.t s.Как мне достичь этой цели.