capawesome-team / capacitor-mlkit

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

bug: #88

Closed LoranRendel closed 1 year ago

LoranRendel commented 1 year ago

Plugin(s)

Did you test the latest version?

Platform(s)

Current behavior

BarcodeScanner.isSupported() results in error Promise <rejected>: Error: This Barcode Scanner plugin method is not available on this platform.

Expected behavior

BarcodeScanner.isSupported() returns: Promise<IsSupportedResult> with {supported: false} as documentation says.

Reproduction

https://github.com/LoranRendel/temporary-mlkit-app

Steps to reproduce

npm start open url check devtools console

Other information

Promise<IsSupportedResult>

Capacitor doctor

[warn] The bundledWebRuntime configuration option has been deprecated. Can be safely deleted. 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/android: not installed @capacitor/ios: not installed @capacitor/core: 5.5.1 @capacitor/cli: 5.5.1

Before submitting

robingenz commented 1 year ago

Available on Android and iOS.

The documentation says that this method is only available on Android and iOS. Therefore, this is not a bug. I just updated the documentation of the isSupported method to be more clear about what exactly happens:

Whether the barcode scanner is available or not by checking if the device has a camera.
LoranRendel commented 1 year ago

Thanks. However this method is probably better to return value on all available platforms?

Now it is required to catch exception if platform is not supported and to validate value if device has no camera.

BarcodeScanner.isSupported().catch(function () { return { supported: false } })
robingenz commented 1 year ago

I think there are arguments for both implementations. Since the plugin does not support the web platform at all, you should not call the plugin on the web. You could simply do it like this:

import { Capacitor } from '@capacitor/core';
import { BarcodeScanner } from '@capacitor-mlkit/barcode-scanning';

const isSupported = async () => {
  if (Capacitor.getPlatform() === 'web') {
    return false;
  }
  const result = await BarcodeScanner.isSupported();
  return result.supported;
};