capawesome-team / capacitor-mlkit

⚡️ ML Kit plugins for Capacitor. Supports Android and iOS.
https://capawesome.io/plugins/mlkit/
Apache License 2.0
147 stars 46 forks source link

bug: On IOS Emulator [error] - Error: Uncaught (in promise): Error: Not available on iOS #89

Closed danisk89 closed 1 year ago

danisk89 commented 1 year ago

Plugin(s)

Did you test the latest version?

Platform(s)

Current behavior

If i try to start a new scan the plugin return me [error] - Error: Uncaught (in promise): Error: Not available on iOS

Expected behavior

I want to use it on iOS. On Android is OK

Reproduction

https://github.com/robingenz/capacitor-mlkit-plugin-demo.git

Steps to reproduce

async scan(): Promise<void> {
    await BarcodeScanner.requestPermissions();

        // Check if the Google ML Kit barcode scanner is available
        await BarcodeScanner.isGoogleBarcodeScannerModuleAvailable().then(async (data) => {
            if (data.available) {
                // Start the barcode scanner
                await this.startScanner().then(async (barcodes) => {
                    var code = barcodes[0].rawValue;
                    console.log(code);
                });
            } else {
                // Install the Google ML Kit barcode scanner
                await BarcodeScanner.installGoogleBarcodeScannerModule().then(async () => {
                    await this.startScanner().then(async (barcodes) => {
                        var code = barcodes[0].rawValue;
                        console.log(code);
                    });
                });
            }
        });
  }

  async startScanner() {
    const { barcodes } = await BarcodeScanner.scan({
        formats: [BarcodeFormat.QrCode, BarcodeFormat.Ean13]
    });
    return barcodes;
}

  async requestPermissions(): Promise<boolean> {
    const { camera } = await BarcodeScanner.requestPermissions();
    return camera === 'granted' || camera === 'limited';
  }

  async presentAlert(): Promise<void> {
    console.log("Non va");
  }

Other information

No response

Capacitor doctor

Latest Dependencies:

@capacitor/cli: 5.5.1 @capacitor/core: 5.5.1 @capacitor/android: 5.5.1 @capacitor/ios: 5.5.1

Installed Dependencies:

@capacitor/cli: 5.4.1 @capacitor/core: 5.4.1 @capacitor/ios: 5.4.1 @capacitor/android: 5.4.1

Before submitting

robingenz commented 1 year ago

isGoogleBarcodeScannerModuleAvailable is not available on iOS, see documentation:

Only available on Android.

Therefore, you should call this method only on Android.

danisk89 commented 1 year ago

Ok, thank you so much. But if i want to use this plugin on ios, how to do? I tried on emulator and doesn't work

robingenz commented 1 year ago

I am not quiet sure what you mean. You can just call the following method (for example) on iOS:

const scan = async () => {
  const { barcodes } = await BarcodeScanner.scan({
    formats: [BarcodeFormat.QrCode],
  });
  return barcodes;
};

But you should call isGoogleBarcodeScannerModuleAvailable only on Android. Therefore just check the platform, see https://capacitorjs.com/docs/core-apis/web#getplatform

danisk89 commented 1 year ago

Ok thanks a lot!! Should it work on ios xcode emulator?

robingenz commented 1 year ago

I haven't tried it.