EddyVerbruggen / nativescript-barcodescanner

🔎 NativeScript QR / barcode (bulk)scanner plugin
MIT License
293 stars 73 forks source link

iOS: requestCameraPermission resolved too early #203

Closed cindy-m closed 5 years ago

cindy-m commented 5 years ago

In our code, when the user uses the barcodescanner for the first time, we call the methode requestCameraPermission. Based on what the user choses, we either go to scan the code or show an alert that the permission was not granted for the functionality. The code looks like this:

this.barcodeScanner.requestCameraPermission()`
      .then(() => this._barcodeScanner.hasCameraPermission()) //workaround for Android
      .then((hasPermission: boolean) => {
        if (hasPermission) {
          this.scanQRCode();
        } else {
          this.showPermissionAlert();
        }
      })
      .catch(() => {
        this.showPermissionAlert();
      });

On iOS, it seems that the first time the user does get the alert with the question if he/she will grant camera acces, but in the code we saw with logging that the promise of the requestCameraPermission was already resolved and it is waiting in the showPermissionAlert function waiting till the alert is closed. So even when the user grants permission, he/she still will get the permission alert right after. Shouldn't it be that the requestCameraPermission promise would only be resolved after the user grants or denies camera acces? Android works correctly as expected, this is only problem on iOS

EddyVerbruggen commented 5 years ago

Hi, did you check the iOS source to see if you can find the cause? Happy to merge a PR as always!

kefahB commented 5 years ago

Hi,

I have same issue with IOS, please try to use camera.requestCameraPermissions() from nativescript-camera instead of barcodescanner.requestCameraPermission()

EddyVerbruggen commented 5 years ago

@kefahB Good point, I'll take a look at their implementation and see if that can be applied to this plugin as well.

kefahB commented 5 years ago

Here is it :

export let requestCameraPermissions = function () {
    return new Promise(function (resolve, reject) {
        let cameraStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo);
        switch (cameraStatus) {
            case AVAuthorizationStatus.NotDetermined: {
                AVCaptureDevice.requestAccessForMediaTypeCompletionHandler(AVMediaTypeVideo, (granted) => {
                    if (granted) {
                        resolve();
                    } else {
                        reject();
                    }
                });
                break;
            }
            case AVAuthorizationStatus.Authorized: {
                resolve();
                break;
            }
            case AVAuthorizationStatus.Restricted:
            case AVAuthorizationStatus.Denied: {
                if (trace.isEnabled()) {
                    trace.write("Application can not access Camera assets.", trace.categories.Debug);
                }
                reject();
                break;
            }
        }
    });
};
EddyVerbruggen commented 5 years ago

Yep, I linked to that bit 😃- cheers mate!