Skip to content

Commit f688ea2

Browse files
author
Dimitar Tachev
authored
Merge pull request #79 from NativeScript/tachev/request-camera-permissions-ios
Request camera permission before opening the camera. Fix issue #51.
2 parents 13db880 + fd7af8a commit f688ea2

File tree

2 files changed

+69
-31
lines changed

2 files changed

+69
-31
lines changed

src/camera.ios.ts

+68-30
Original file line numberDiff line numberDiff line change
@@ -176,38 +176,76 @@ export let isAvailable = function () {
176176
};
177177

178178
export let requestPermissions = function () {
179-
return new Promise(function(resolve, reject) {
180-
let authStatus = PHPhotoLibrary.authorizationStatus();
181-
switch (authStatus) {
182-
case PHAuthorizationStatus.NotDetermined: {
183-
PHPhotoLibrary.requestAuthorization((auth) => {
184-
if (auth === PHAuthorizationStatus.Authorized) {
185-
if (trace.isEnabled()) {
186-
trace.write("Application can access photo library assets.", trace.categories.Debug);
187-
}
188-
resolve();
189-
}
190-
else {
179+
return new Promise(function (resolve, reject) {
180+
requestPhotosPermissions().then(() => {
181+
requestCameraPermissions().then(resolve, reject);
182+
}, reject);
183+
});
184+
};
185+
186+
let requestPhotosPermissions = function () {
187+
return new Promise(function (resolve, reject) {
188+
let authStatus = PHPhotoLibrary.authorizationStatus();
189+
switch (authStatus) {
190+
case PHAuthorizationStatus.NotDetermined: {
191+
PHPhotoLibrary.requestAuthorization((auth) => {
192+
if (auth === PHAuthorizationStatus.Authorized) {
193+
if (trace.isEnabled()) {
194+
trace.write("Application can access photo library assets.", trace.categories.Debug);
195+
}
196+
resolve();
197+
}
198+
else {
199+
reject();
200+
}
201+
});
202+
break;
203+
}
204+
case PHAuthorizationStatus.Authorized: {
205+
if (trace.isEnabled()) {
206+
trace.write("Application can access photo library assets.", trace.categories.Debug);
207+
}
208+
resolve();
209+
break;
210+
}
211+
case PHAuthorizationStatus.Restricted:
212+
case PHAuthorizationStatus.Denied: {
213+
if (trace.isEnabled()) {
214+
trace.write("Application can not access photo library assets.", trace.categories.Debug);
215+
}
191216
reject();
192-
}
193-
});
194-
break;
195-
}
196-
case PHAuthorizationStatus.Authorized: {
197-
if (trace.isEnabled()) {
198-
trace.write("Application can access photo library assets.", trace.categories.Debug);
199-
}
200-
resolve();
201-
break;
217+
break;
218+
}
202219
}
203-
case PHAuthorizationStatus.Restricted:
204-
case PHAuthorizationStatus.Denied: {
205-
if (trace.isEnabled()) {
206-
trace.write("Application can not access photo library assets.", trace.categories.Debug);
207-
}
208-
reject();
209-
break;
220+
});
221+
};
222+
223+
let requestCameraPermissions = function () {
224+
return new Promise(function (resolve, reject) {
225+
let cameraStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo);
226+
switch (cameraStatus) {
227+
case AVAuthorizationStatus.NotDetermined: {
228+
AVCaptureDevice.requestAccessForMediaTypeCompletionHandler(AVMediaTypeVideo, (granted) => {
229+
if (granted) {
230+
resolve();
231+
} else {
232+
reject();
233+
}
234+
});
235+
break;
236+
}
237+
case AVAuthorizationStatus.Authorized: {
238+
resolve();
239+
break;
240+
}
241+
case AVAuthorizationStatus.Restricted:
242+
case AVAuthorizationStatus.Denied: {
243+
if (trace.isEnabled()) {
244+
trace.write("Application can not access Camera assets.", trace.categories.Debug);
245+
}
246+
reject();
247+
break;
248+
}
210249
}
211-
}
212250
});
213251
};

src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-camera",
3-
"version": "3.2.0",
3+
"version": "3.2.1",
44
"description": "Provides API for using device camera",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)