Ionic project: communication between Swift and Typescript layers

65 Views Asked by At

I need some help with this problem. I have an Ionic project (v6) with Capacitor (v4.4.0) and I would like to Know if there is a way to communicate betweeen Swift layer (something happens in AppDelegate.swift) to Typescript layer (a service class, for example).

In my AppDelegate.swift there is a method that executes when something happens, and I would like to be able to communicate that something has happened to my Typescript class, in order to do some stuff there.

I use NotifcationCenter in order to do that. Inside a function in AppDelegate.swift I have the following code:

NotificationCenter.default.post(name: NSNotification.Name("somethingHappened"), object: nil, userInfo: userInfo)

And then in my typescript class I have something like that:

import { Plugins } from '@capacitor/core';
const { App } = Plugins;

export class NotificationService {
  constructor() {
    this.initialize();
  }

  private initialize() {
    App.addListener('somethingHappened', (notification: any) => {
      console.log('Testing Listener. Somethig Happened: ', notification);
      // Do other things...
    });
  } 
} 

But when I execute the app on the iphone and I check the logs on Xcode, listener never fires up.

I have read about using a bridge between Swift and Typescript, but I think brige it is not compatible with Capacitor 4.4.0.

I try to commuicate between AppDelegate.swift (swift layer) and Typescript layer in an Ionic project, but I don't find the way to do that.

Any help or advice will be appreciated. Thanks!

1

There are 1 best solutions below

2
StackoverBlows On

You need to create a capacitor plugin to be able to communicate with native layer.