未触发app.component.ts文件中的Ionic3 iOS插件回调响应

时间:2019-02-06 10:09:26

标签: ios ionic3 cordova-plugins phonegap ionic-plugins

我正在使用Ionic iOS插件,并希望将回调从自定义的Objective-C文件发送到应用的components.ts文件。

这是我的代码示例:

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];
}

从iOS库内部执行 MYCustomiOSFile2 时,自定义通知触发了“ 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");
          }
          );
        });
      }

我要在 app.component.t 文件中获取发布的数据(即{“ MyKey1”:“ MyValue1”})。我怎样才能实现这个目标。

0 个答案:

没有答案