Skip to content

Commit 58ad94f

Browse files
Dimitar TachevDimitar Tachev
Dimitar Tachev
authored and
Dimitar Tachev
committed
Request camera permission before opening the camera. Fix issue #51.
1 parent 13db880 commit 58ad94f

File tree

2 files changed

+64
-32
lines changed

2 files changed

+64
-32
lines changed

src/camera.ios.ts

+63-31
Original file line numberDiff line numberDiff line change
@@ -176,38 +176,70 @@ 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+
let cameraStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo);
182+
switch (cameraStatus) {
183+
case AVAuthorizationStatus.NotDetermined: {
184+
AVCaptureDevice.requestAccessForMediaTypeCompletionHandler(AVMediaTypeVideo, (granted) => {
185+
if (granted) {
186+
resolve();
187+
} else {
188+
reject();
189+
}
190+
});
191+
break;
192+
}
193+
case AVAuthorizationStatus.Authorized: {
194+
resolve();
195+
break;
196+
}
197+
case AVAuthorizationStatus.Restricted:
198+
case AVAuthorizationStatus.Denied: {
199+
if (trace.isEnabled()) {
200+
trace.write("Application can not access Camera assets.", trace.categories.Debug);
201+
}
202+
reject();
203+
break;
204+
}
205+
}
206+
}, (error) => reject(error));
207+
});
208+
};
209+
210+
let requestPhotosPermissions = function () {
211+
return new Promise(function (resolve, reject) {
212+
let authStatus = PHPhotoLibrary.authorizationStatus();
213+
switch (authStatus) {
214+
case PHAuthorizationStatus.NotDetermined: {
215+
PHPhotoLibrary.requestAuthorization((auth) => {
216+
if (auth === PHAuthorizationStatus.Authorized) {
217+
if (trace.isEnabled()) {
218+
trace.write("Application can access photo library assets.", trace.categories.Debug);
219+
}
220+
resolve();
221+
}
222+
else {
223+
reject();
224+
}
225+
});
226+
break;
227+
}
228+
case PHAuthorizationStatus.Authorized: {
229+
if (trace.isEnabled()) {
230+
trace.write("Application can access photo library assets.", trace.categories.Debug);
231+
}
232+
resolve();
233+
break;
234+
}
235+
case PHAuthorizationStatus.Restricted:
236+
case PHAuthorizationStatus.Denied: {
237+
if (trace.isEnabled()) {
238+
trace.write("Application can not access photo library assets.", trace.categories.Debug);
239+
}
191240
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;
202-
}
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;
241+
break;
242+
}
210243
}
211-
}
212244
});
213245
};

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)