Skip to content

Commit fd7129c

Browse files
committed
feat: use new calendar and reminders access APIs
1 parent e406d65 commit fd7129c

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ function askForFoldersAccess(folder) {
3636
return permissions.askForFoldersAccess.call(this, folder)
3737
}
3838

39+
function askForCalendarAccess(type) {
40+
if (!['write-only', 'full'].includes(type)) {
41+
throw new TypeError(`${type} must be one of either 'write-only' or 'full'`)
42+
}
43+
44+
return permissions.askForCalendarAccess.call(this, type)
45+
}
46+
3947
function askForScreenCaptureAccess(openPreferences = false) {
4048
if (typeof openPreferences !== 'boolean') {
4149
throw new TypeError('openPreferences must be a boolean')
@@ -62,7 +70,7 @@ function askForInputMonitoringAccess(accessLevel = 'listen') {
6270

6371
module.exports = {
6472
askForAccessibilityAccess: permissions.askForAccessibilityAccess,
65-
askForCalendarAccess: permissions.askForCalendarAccess,
73+
askForCalendarAccess: askForCalendarAccess,
6674
askForCameraAccess: permissions.askForCameraAccess,
6775
askForContactsAccess: permissions.askForContactsAccess,
6876
askForFoldersAccess,

permissions.mm

+49-23
Original file line numberDiff line numberDiff line change
@@ -510,17 +510,38 @@ bool HasOpenSystemPreferencesDialog() {
510510
env, Napi::Function::New(env, NoOp), "calendarCallback", 0, 1);
511511

512512
__block Napi::ThreadSafeFunction tsfn = ts_fn;
513-
[[EKEventStore new]
514-
requestAccessToEntityType:EKEntityTypeEvent
515-
completion:^(BOOL granted, NSError *error) {
516-
auto callback = [=](Napi::Env env, Napi::Function js_cb,
517-
const char *granted) {
518-
deferred.Resolve(Napi::String::New(env, granted));
519-
};
520-
tsfn.BlockingCall(granted ? "authorized" : "denied",
521-
callback);
522-
tsfn.Release();
523-
}];
513+
if (@available(macOS 14.0, *)) {
514+
const std::string type = info[0].As<Napi::String>().Utf8Value();
515+
516+
EKEventStoreRequestAccessCompletionHandler handler =
517+
^(BOOL granted, NSError *error) {
518+
auto callback = [=](Napi::Env env, Napi::Function js_cb,
519+
const char *granted) {
520+
deferred.Resolve(Napi::String::New(env, granted));
521+
};
522+
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
523+
tsfn.Release();
524+
};
525+
526+
if (type == "full") {
527+
[[EKEventStore new] requestFullAccessToEventsWithCompletion:handler];
528+
} else {
529+
[[EKEventStore new] requestWriteOnlyAccessToEventsWithCompletion:handler];
530+
}
531+
} else {
532+
[[EKEventStore new]
533+
requestAccessToEntityType:EKEntityTypeEvent
534+
completion:^(BOOL granted, NSError *error) {
535+
auto callback = [=](Napi::Env env,
536+
Napi::Function js_cb,
537+
const char *granted) {
538+
deferred.Resolve(Napi::String::New(env, granted));
539+
};
540+
tsfn.BlockingCall(granted ? "authorized" : "denied",
541+
callback);
542+
tsfn.Release();
543+
}];
544+
}
524545

525546
return deferred.Promise();
526547
}
@@ -533,18 +554,23 @@ bool HasOpenSystemPreferencesDialog() {
533554
env, Napi::Function::New(env, NoOp), "remindersCallback", 0, 1);
534555

535556
__block Napi::ThreadSafeFunction tsfn = ts_fn;
536-
[[EKEventStore new]
537-
requestAccessToEntityType:EKEntityTypeReminder
538-
completion:^(BOOL granted, NSError *error) {
539-
auto callback = [=](Napi::Env env,
540-
Napi::Function prom_cb,
541-
const char *granted) {
542-
deferred.Resolve(Napi::String::New(env, granted));
543-
};
544-
tsfn.BlockingCall(granted ? "authorized" : "denied",
545-
callback);
546-
tsfn.Release();
547-
}];
557+
558+
EKEventStoreRequestAccessCompletionHandler handler =
559+
^(BOOL granted, NSError *error) {
560+
auto callback = [=](Napi::Env env, Napi::Function prom_cb,
561+
const char *granted) {
562+
deferred.Resolve(Napi::String::New(env, granted));
563+
};
564+
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
565+
tsfn.Release();
566+
};
567+
568+
if (@available(macOS 14.0, *)) {
569+
[[EKEventStore new] requestFullAccessToRemindersWithCompletion:handler];
570+
} else {
571+
[[EKEventStore new] requestAccessToEntityType:EKEntityTypeReminder
572+
completion:handler];
573+
}
548574

549575
return deferred.Promise();
550576
}

0 commit comments

Comments
 (0)