Skip to content

Commit 2c33778

Browse files
committed
feat: add permissions.askForLocationAccess()
1 parent a37279c commit 2c33778

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function askForContactsAccess(): Promise<Omit<PermissionType, 'restricted
88
export function askForFoldersAccess(): Promise<Omit<PermissionType, 'restricted'>>
99
export function askForFullDiskAccess(): undefined
1010
export function askForInputMonitoringAccess(accessType?: 'listen' | 'post'): Promise<Omit<PermissionType, 'restricted'>>
11+
export function askForLocationAccess(accessType?: 'when-in-use' | 'always'): Promise<Omit<PermissionType, 'restricted'>>
1112
export function askForMicrophoneAccess(): Promise<PermissionType>
1213
export function askForPhotosAccess(accessType?: 'add-only' | 'read-write'): Promise<PermissionType>
1314
export function askForRemindersAccess(): Promise<Omit<PermissionType, 'restricted'>>

index.js

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ function askForCalendarAccess(accessLevel = 'write-only') {
4444
return permissions.askForCalendarAccess.call(this, accessLevel)
4545
}
4646

47+
function askForLocationAccess(accessLevel = 'when-in-use') {
48+
if (!['when-in-use', 'always'].includes(accessLevel)) {
49+
throw new TypeError(`${accessLevel} must be one of either 'when-in-use' or 'always'`)
50+
}
51+
52+
return permissions.askForLocationAccess.call(this, accessLevel)
53+
}
54+
4755
function askForScreenCaptureAccess(openPreferences = false) {
4856
if (typeof openPreferences !== 'boolean') {
4957
throw new TypeError('openPreferences must be a boolean')
@@ -71,6 +79,7 @@ function askForInputMonitoringAccess(accessLevel = 'listen') {
7179
module.exports = {
7280
askForAccessibilityAccess: permissions.askForAccessibilityAccess,
7381
askForCalendarAccess: askForCalendarAccess,
82+
askForLocationAccess: askForLocationAccess,
7483
askForCameraAccess: permissions.askForCameraAccess,
7584
askForContactsAccess: permissions.askForContactsAccess,
7685
askForFoldersAccess,

permissions.mm

+21
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,25 @@ void AskForScreenCaptureAccess(const Napi::CallbackInfo &info) {
846846
}
847847
}
848848

849+
// Request Location access.
850+
void AskForLocationAccess(const Napi::CallbackInfo &info) {
851+
if (@available(macOS 10.15, *)) {
852+
std::string auth_status = LocationAuthStatus();
853+
854+
if (auth_status == kNotDetermined) {
855+
CLLocationManager *location_manager = [[CLLocationManager alloc] init];
856+
const std::string access_level = info[0].As<Napi::String>().Utf8Value();
857+
if (access_level == "always") {
858+
[location_manager requestAlwaysAuthorization];
859+
} else if (access_level == "when-in-use") {
860+
[location_manager requestWhenInUseAuthorization];
861+
}
862+
} else if (auth_status == kDenied) {
863+
OpenPrefPane("Privacy_Location");
864+
}
865+
}
866+
}
867+
849868
// Request Accessibility Access.
850869
void AskForAccessibilityAccess(const Napi::CallbackInfo &info) {
851870
NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt : @(NO)};
@@ -872,6 +891,8 @@ void AskForAccessibilityAccess(const Napi::CallbackInfo &info) {
872891
Napi::Function::New(env, AskForFullDiskAccess));
873892
exports.Set(Napi::String::New(env, "askForCameraAccess"),
874893
Napi::Function::New(env, AskForCameraAccess));
894+
exports.Set(Napi::String::New(env, "askForLocationAccess"),
895+
Napi::Function::New(env, AskForLocationAccess));
875896
exports.Set(Napi::String::New(env, "askForMicrophoneAccess"),
876897
Napi::Function::New(env, AskForMicrophoneAccess));
877898
exports.Set(Napi::String::New(env, "askForMusicLibraryAccess"),

0 commit comments

Comments
 (0)