From 92a36d1d0027127958f1ec865fb945abec1be51f Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Fri, 7 Jun 2019 14:49:24 +0200 Subject: [PATCH] iOS: requestCameraPermission resolved too early #203 --- demo/app/main-view-model.ts | 8 +++----- src/barcodescanner.ios.ts | 26 ++++++++++++++++++++++---- src/package.json | 2 +- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/demo/app/main-view-model.ts b/demo/app/main-view-model.ts index 62539cd..07d5e7f 100644 --- a/demo/app/main-view-model.ts +++ b/demo/app/main-view-model.ts @@ -36,11 +36,9 @@ export class HelloWorldModel extends Observable { } public doRequestCameraPermission() { - this.barcodeScanner.requestCameraPermission().then( - function () { - console.log("Camera permission requested"); - } - ); + this.barcodeScanner.requestCameraPermission() + .then(() => console.log("Camera permission granted")) + .catch(() => console.log("Camera permission not granted")); } public doScanWithBackCamera() { diff --git a/src/barcodescanner.ios.ts b/src/barcodescanner.ios.ts index 49f46d2..e90360b 100644 --- a/src/barcodescanner.ios.ts +++ b/src/barcodescanner.ios.ts @@ -165,9 +165,27 @@ export class BarcodeScanner { } public requestCameraPermission(): Promise { - return new Promise((resolve) => { - // this will trigger the prompt on iOS 10+ - resolve(QRCodeReader.isAvailable()); + return new Promise((resolve, reject) => { + const cameraStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo); + + switch (cameraStatus) { + case AVAuthorizationStatus.NotDetermined: { + AVCaptureDevice.requestAccessForMediaTypeCompletionHandler( + AVMediaTypeVideo, + granted => granted ? resolve() : reject() + ); + break; + } + case AVAuthorizationStatus.Authorized: { + resolve(); + break; + } + case AVAuthorizationStatus.Restricted: + case AVAuthorizationStatus.Denied: { + reject(); + break; + } + } }); } @@ -335,7 +353,7 @@ const shouldReturnEAN13AsUPCA = (barcodeFormat: BarcodeFormat, value: string, re return barcodeFormat === "EAN_13" && value.indexOf("0") === 0; // why not add the line below? Well, see https://github.com/EddyVerbruggen/nativescript-barcodescanner/issues/176 - // && (!requestedFormats || requestedFormats.indexOf("UPC_A") > -1); + // && (!requestedFormats || requestedFormats.indexOf("UPC_A") > -1); }; const getBarcodeFormat = (nativeFormat: string): BarcodeFormat => { diff --git a/src/package.json b/src/package.json index 6a9c9ba..957e90f 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-barcodescanner", - "version": "3.1.3", + "version": "3.2.0", "description": "Scan QR/barcodes with your NativeScript app.", "main": "barcodescanner", "typings": "index.d.ts",