Skip to content

Commit

Permalink
Merge pull request #79 from NativeScript/tachev/request-camera-permis…
Browse files Browse the repository at this point in the history
…sions-ios

Request camera permission before opening the camera. Fix issue #51.
  • Loading branch information
Dimitar Tachev authored Dec 6, 2017
2 parents 13db880 + fd7af8a commit f688ea2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 31 deletions.
98 changes: 68 additions & 30 deletions src/camera.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
});
};
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-camera",
"version": "3.2.0",
"version": "3.2.1",
"description": "Provides API for using device camera",
"repository": {
"type": "git",
Expand Down

0 comments on commit f688ea2

Please sign in to comment.