Passing Data from AppDelegate.swift to Angular with Capacitor Plugin - "UNIMPLEMENTED" Error

Discussão em 'Angular' iniciado por engage_roll, Outubro 18, 2024.

  1. engage_roll

    engage_roll Guest

    I have an Angular application that I converted into an iOS app using Capacitor. I’m trying to implement a feature to retrieve health data using Apple HealthKit and pass that data from the AppDelegate.swift file to my Angular app. I'm using a custom Capacitor plugin to do this, but I'm encountering an error when setting up the listener in Angular.

    Here’s what I’ve tried:

    Health Data Plugin (Swift):

    import Capacitor

    @objc(HealthDataPlugin)
    public class HealthDataPlugin: CAPPlugin {

    static let shared = HealthDataPlugin() // Singleton instance

    @objc func sendHealthDataToAngular(data: [String: Any]) {
    print("sendHealthDataToAngular called with data: \(data)")

    guard !data.isEmpty else {
    print("Error: No data provided to sendHealthDataToAngular.")
    return
    }

    do {
    self.notifyListeners("healthDataReceived", data: data)
    } catch {
    print("Error: Failed to notify listeners - \(error.localizedDescription)")
    }
    }

    @objc func echo(_ call: CAPPluginCall) {
    let value = call.getString("value") ?? ""
    call.resolve([
    "value": value
    ])
    }
    }


    Call in AppDelegate.swift:

    HealthDataPlugin.shared.sendHealthDataToAngular(data: dataToSend)


    Angular TypeScript (Listener Setup):

    async setupHealthDataListener() {
    try {
    console.log("Setting up health data listener...");
    const eventListener = await (HealthDataPlugin as any).addListener('healthDataReceived', (eventData: any) => {
    console.log('Health Data Received:', eventData);
    });

    console.log("Health data listener set up successfully:", eventListener);
    } catch (error) {
    console.error("Error setting up health data listener:", error);
    }
    }


    Plugin Registration in Angular (health-kit-plugin.ts):

    import { registerPlugin } from '@capacitor/core';

    const HealthDataPlugin = registerPlugin('HealthDataPlugin');

    export default HealthDataPlugin;


    capacitor.config.ts

    import { CapacitorConfig } from '@capacitor/cli';

    const config: CapacitorConfig = {
    appId: 'app.rapidhealth',
    appName: 'Rapid Health',
    webDir: './dist/rapid',
    server: {
    androidScheme: 'https'
    },
    plugins: {
    HealthDataPlugin: {},
    }
    };

    export default config;




    Error Log from Xcode:

    ⚡️ [log] - Setting up health data listener...
    ⚡️ [error] - Error setting up health data listener: {"code":"UNIMPLEMENTED"}


    Note: I have tried running npx cap migrate which upgrading my capacitor version to 6.0.0 but this hasn't solved my issue.

    It seems like the listener isn’t being recognised or implemented properly. What could be causing this "UNIMPLEMENTED" error, and how can I correctly pass the health data from AppDelegate.swift to my Angular app using Capacitor?

    Continue reading...

Compartilhe esta Página