NativeScript/mlkit

Not auto-requesting camera permissions

boutier opened this issue · 2 comments

I had a few difficulties to make this plugin work (with angular). The last one was that I didn't think about explicitly asking camera permission to the user. I guess this should (at least) be mentioned in the README page.

    "@angular/…": "~15.2.0",
    "@nativescript/angular": "^15.0.0",
    "@nativescript/camera": "~5.0.15",  # I used this to require camera permissions
    "@nativescript/core": "~8.5.0",
    "@nativescript/mlkit-barcode-scanning": "~2.0.0",
    "@nativescript/mlkit-core": "~2.0.0",

I tried to create a fresh NativeScript app, with ns create test --ng and the mlkit packages above.

And then I just had a black screen. But it worked from ns-preview. So I realized that ns-preview actually ask for camera permission, and so it worked after adding @nativescript/camera to my package.json and making something like:

import * as Camera from '@nativescript/camera';

export class BarcodeComponent {
  havePermission = false;

  constructor() {
    Camera.requestPermissions().then((v) => (this.havePermission = true));
  }
}
  <MLKitView *ngIf="havePermission" ...

Notes:

  • I was not able to add (onLoaded)="$event.object.requestCameraPermission() to MLKitView.
  • I didn't try to use a @ViewChild

Clearly, it would be great to add some documentation here https://github.com/NativeScript/mlkit/blob/main/packages/mlkit-core/README.md

I didn't propose a patch request because it may not be related to Angular only… but the angular section could be:

#### Angular

```ts
import { MLKitView } from "@nativescript/mlkit-core";
import { MLKitModule } from '@nativescript/mlkit-core/angular';

registerElement("MLKitView", () => MLKitView);

@NgModule({
    imports: [
    MLKitModule
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [AppComponent]
})
```

```html
<MLKitView
*ngIf="haveCameraPermission"
cameraPosition="back"
detectionType="all" 
(detection)="onDetection($event)"
></MLKitView>
```

You should also request for camera permission:
```ts
import * as Camera from '@nativescript/camera';

[…] SomeComponent() {
  haveCameraPermission = false;
  constructor() {
    Camera.requestPermissions().then((v) => (this.havePermission = true));
```

A last thing: thanks a lot for this plugin!

Hey @boutier yes I should add that to the readme, this is how I did it in the demo. Another thing I can do is add a static method to request permission

Hi, thx @boutier for the code, it helps me a lot... it works till android 12. But 13 is not using WRITE_EXTERNAL_STORAGE permission anymore, so that permission is never set to true... and so scanner is never visible. Any help or suggestions on that? Pretty please :) I feel like I am the only one using this plugin. Thank You!