From fd7af8a58eb760e5639ae5cc1cdd5e9775be6afb Mon Sep 17 00:00:00 2001 From: Dimitar Tachev Date: Tue, 5 Dec 2017 18:22:53 +0200 Subject: [PATCH] Request camera permission before opening the camera. Fix issue #51. --- src/camera.ios.ts | 98 ++++++++++++++++++++++++++++++++--------------- src/package.json | 2 +- 2 files changed, 69 insertions(+), 31 deletions(-) diff --git a/src/camera.ios.ts b/src/camera.ios.ts index 87447bd..d1d2956 100644 --- a/src/camera.ios.ts +++ b/src/camera.ios.ts @@ -176,38 +176,76 @@ export let isAvailable = function () { }; export let requestPermissions = function () { - return new Promise(function(resolve, reject) { - let authStatus = PHPhotoLibrary.authorizationStatus(); - switch (authStatus) { - case PHAuthorizationStatus.NotDetermined: { - PHPhotoLibrary.requestAuthorization((auth) => { - if (auth === PHAuthorizationStatus.Authorized) { - if (trace.isEnabled()) { - trace.write("Application can access photo library assets.", trace.categories.Debug); - } - resolve(); - } - else { + return new Promise(function (resolve, reject) { + requestPhotosPermissions().then(() => { + requestCameraPermissions().then(resolve, reject); + }, reject); + }); +}; + +let requestPhotosPermissions = function () { + return new Promise(function (resolve, reject) { + let authStatus = PHPhotoLibrary.authorizationStatus(); + switch (authStatus) { + case PHAuthorizationStatus.NotDetermined: { + PHPhotoLibrary.requestAuthorization((auth) => { + if (auth === PHAuthorizationStatus.Authorized) { + if (trace.isEnabled()) { + trace.write("Application can access photo library assets.", trace.categories.Debug); + } + resolve(); + } + else { + reject(); + } + }); + break; + } + case PHAuthorizationStatus.Authorized: { + if (trace.isEnabled()) { + trace.write("Application can access photo library assets.", trace.categories.Debug); + } + resolve(); + break; + } + case PHAuthorizationStatus.Restricted: + case PHAuthorizationStatus.Denied: { + if (trace.isEnabled()) { + trace.write("Application can not access photo library assets.", trace.categories.Debug); + } reject(); - } - }); - break; - } - case PHAuthorizationStatus.Authorized: { - if (trace.isEnabled()) { - trace.write("Application can access photo library assets.", trace.categories.Debug); - } - resolve(); - break; + break; + } } - case PHAuthorizationStatus.Restricted: - case PHAuthorizationStatus.Denied: { - if (trace.isEnabled()) { - trace.write("Application can not access photo library assets.", trace.categories.Debug); - } - reject(); - break; + }); +}; + +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; + } } - } }); }; diff --git a/src/package.json b/src/package.json index 428a1d4..601ae21 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-camera", - "version": "3.2.0", + "version": "3.2.1", "description": "Provides API for using device camera", "repository": { "type": "git",