Skip to content

Commit

Permalink
iOS: requestCameraPermission resolved too early #203
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Jun 7, 2019
1 parent a06f91c commit 92a36d1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
8 changes: 3 additions & 5 deletions demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
26 changes: 22 additions & 4 deletions src/barcodescanner.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,27 @@ export class BarcodeScanner {
}

public requestCameraPermission(): Promise<boolean> {
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;
}
}
});
}

Expand Down Expand Up @@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 92a36d1

Please sign in to comment.