|
6 | 6 | #import <Contacts/Contacts.h>
|
7 | 7 | #import <CoreLocation/CoreLocation.h>
|
8 | 8 | #import <EventKit/EventKit.h>
|
9 |
| -#import <Photos/Photos.h> |
10 | 9 | #import <Foundation/Foundation.h>
|
| 10 | +#import <Photos/Photos.h> |
11 | 11 | #import <pwd.h>
|
12 | 12 |
|
13 | 13 | /***** HELPER FUNCTIONS *****/
|
|
249 | 249 | Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
|
250 | 250 | env, Napi::Function::New(env, NoOp), "contactsCallback", 0, 1);
|
251 | 251 |
|
252 |
| - __block Napi::ThreadSafeFunction tsfn = ts_fn; |
253 | 252 | if (@available(macOS 10.11, *)) {
|
| 253 | + __block Napi::ThreadSafeFunction tsfn = ts_fn; |
254 | 254 | CNContactStore *store = [CNContactStore new];
|
255 | 255 | [store requestAccessForEntityType:CNEntityTypeContacts
|
256 | 256 | completionHandler:^(BOOL granted, NSError *error) {
|
|
263 | 263 | tsfn.Release();
|
264 | 264 | }];
|
265 | 265 | } else {
|
| 266 | + ts_fn.Release(); |
266 | 267 | deferred.Resolve(Napi::String::New(env, "authorized"));
|
267 | 268 | }
|
268 | 269 |
|
@@ -355,11 +356,57 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
|
355 | 356 |
|
356 | 357 | [workspace openURL:[NSURL URLWithString:pref_string]];
|
357 | 358 |
|
| 359 | + ts_fn.Release(); |
| 360 | + deferred.Resolve(Napi::String::New(env, "denied")); |
| 361 | + } else { |
| 362 | + ts_fn.Release(); |
| 363 | + deferred.Resolve(Napi::String::New(env, auth_status)); |
| 364 | + } |
| 365 | + } else { |
| 366 | + ts_fn.Release(); |
| 367 | + deferred.Resolve(Napi::String::New(env, "authorized")); |
| 368 | + } |
| 369 | + |
| 370 | + return deferred.Promise(); |
| 371 | +} |
| 372 | + |
| 373 | +// Request Photos access. |
| 374 | +Napi::Promise AskForPhotosAccess(const Napi::CallbackInfo &info) { |
| 375 | + Napi::Env env = info.Env(); |
| 376 | + Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env); |
| 377 | + Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New( |
| 378 | + env, Napi::Function::New(env, NoOp), "photosCallback", 0, 1); |
| 379 | + |
| 380 | + if (@available(macOS 10.13, *)) { |
| 381 | + std::string auth_status = PhotosAuthStatus(); |
| 382 | + |
| 383 | + if (auth_status == "not determined") { |
| 384 | + __block Napi::ThreadSafeFunction tsfn = ts_fn; |
| 385 | + [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { |
| 386 | + auto callback = [=](Napi::Env env, Napi::Function js_cb, |
| 387 | + const char *granted) { |
| 388 | + deferred.Resolve(Napi::String::New(env, granted)); |
| 389 | + }; |
| 390 | + tsfn.BlockingCall( |
| 391 | + status == PHAuthorizationStatusAuthorized ? "authorized" : "denied", |
| 392 | + callback); |
| 393 | + tsfn.Release(); |
| 394 | + }]; |
| 395 | + } else if (auth_status == "denied") { |
| 396 | + NSWorkspace *workspace = [[NSWorkspace alloc] init]; |
| 397 | + NSString *pref_string = @"x-apple.systempreferences:com.apple.preference." |
| 398 | + @"security?Privacy_Photos"; |
| 399 | + |
| 400 | + [workspace openURL:[NSURL URLWithString:pref_string]]; |
| 401 | + |
| 402 | + ts_fn.Release(); |
358 | 403 | deferred.Resolve(Napi::String::New(env, "denied"));
|
359 | 404 | } else {
|
| 405 | + ts_fn.Release(); |
360 | 406 | deferred.Resolve(Napi::String::New(env, auth_status));
|
361 | 407 | }
|
362 | 408 | } else {
|
| 409 | + ts_fn.Release(); |
363 | 410 | deferred.Resolve(Napi::String::New(env, "authorized"));
|
364 | 411 | }
|
365 | 412 |
|
@@ -397,11 +444,14 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
|
397 | 444 |
|
398 | 445 | [workspace openURL:[NSURL URLWithString:pref_string]];
|
399 | 446 |
|
| 447 | + ts_fn.Release(); |
400 | 448 | deferred.Resolve(Napi::String::New(env, "denied"));
|
401 | 449 | } else {
|
| 450 | + ts_fn.Release(); |
402 | 451 | deferred.Resolve(Napi::String::New(env, auth_status));
|
403 | 452 | }
|
404 | 453 | } else {
|
| 454 | + ts_fn.Release(); |
405 | 455 | deferred.Resolve(Napi::String::New(env, "authorized"));
|
406 | 456 | }
|
407 | 457 |
|
@@ -459,6 +509,8 @@ void AskForAccessibilityAccess(const Napi::CallbackInfo &info) {
|
459 | 509 | Napi::Function::New(env, AskForCameraAccess));
|
460 | 510 | exports.Set(Napi::String::New(env, "askForMicrophoneAccess"),
|
461 | 511 | Napi::Function::New(env, AskForMicrophoneAccess));
|
| 512 | + exports.Set(Napi::String::New(env, "askForPhotosAccess"), |
| 513 | + Napi::Function::New(env, AskForPhotosAccess)); |
462 | 514 | exports.Set(Napi::String::New(env, "askForScreenCaptureAccess"),
|
463 | 515 | Napi::Function::New(env, AskForScreenCaptureAccess));
|
464 | 516 | exports.Set(Napi::String::New(env, "askForAccessibilityAccess"),
|
|
0 commit comments