Skip to content

Commit 9f7d856

Browse files
committed
feat: implement askForContactsAccess
1 parent f46333d commit 9f7d856

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This native Node.js module allows you to manage an app's access to:
1616

1717
## `permissions.getAuthStatus(type)`
1818

19-
* `type` - The type of system component to which you are requesting access. Can be one of 'contacts', 'full-disk-access', 'photos', 'reminders', or 'calendar'.
19+
* `type` String - The type of system component to which you are requesting access. Can be one of 'contacts', 'full-disk-access', 'photos', 'reminders', or 'calendar'.
2020

2121
Returns `String` - Can be one of 'Not Determined', 'Denied', 'Authorized', or 'Restricted'.
2222

@@ -29,3 +29,24 @@ Return Value Descriptions:
2929
* 'Authorized' - The application is authorized to access `type` data.
3030

3131
**Note:** Access to 'contacts' will always return a status of 'Authorized' prior to macOS 10.13 High Sierra, as access to contacts was unilaterally allowed until that version.
32+
33+
## `permissions.askForContactsAccess(callback)`
34+
35+
* `callback` Function
36+
* `error` String | null - An error in performing the request, if one occurred.
37+
* `status` String - Whether or not the request succeeded or failed; can be 'authorized' or 'denied'.
38+
39+
In your app, you should put the reason you're requesting to manipulate user's contacts database in your `Info.plist` like so:
40+
41+
```
42+
<key>NSContactsUsageDescription</key>
43+
<string>Your reason for wanting to access the Contact store</string>
44+
```
45+
46+
```js
47+
const { askForContactsAccess } = require('node-mac-permissions')
48+
49+
askForContactsAccess((err, status) => {
50+
console.log(`Access to Contacts was ${status}`)
51+
})
52+
```

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ function getAuthStatus(type) {
1010
}
1111

1212
module.exports = {
13-
getAuthStatus
13+
getAuthStatus,
14+
askForContactsAccess: permissions.askForContactsAccess
1415
}

permissions.mm

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
return auth_status;
4343
}
4444

45-
// Returns a status indicating whether or not the user has authorized Calendar access
45+
// Returns a status indicating whether or not the user has authorized Calendar/Reminders access
4646
std::string EventAuthStatus(std::string type) {
4747
std::string auth_status = "Not Determined";
4848

@@ -125,11 +125,34 @@
125125
return Napi::Value::From(env, auth_status);
126126
}
127127

128+
// Request access to the Contacts store.
129+
void AskForContactsAccess(const Napi::CallbackInfo &info) {
130+
Napi::Env env = info.Env();
131+
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(env, info[0].As<Napi::Function>(),
132+
"Resource Name", 0, 1, [](Napi::Env){});
133+
134+
if (@available(macOS 10.11, *)) {
135+
CNContactStore *store = [CNContactStore new];
136+
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError* error) {
137+
auto callback = [](Napi::Env env, Napi::Function js_cb, const char* granted) {
138+
js_cb.Call({Napi::String::New(env, granted)});
139+
};
140+
ts_fn.BlockingCall(granted ? "authorized" : "denied", callback);
141+
}];
142+
} else {
143+
Napi::FunctionReference fn = Napi::Persistent(info[0].As<Napi::Function>());
144+
fn.Call({Napi::String::New(env, "authorized")});
145+
}
146+
}
147+
128148
// Initializes all functions exposed to JS
129149
Napi::Object Init(Napi::Env env, Napi::Object exports) {
130150
exports.Set(
131151
Napi::String::New(env, "getAuthStatus"), Napi::Function::New(env, GetAuthStatus)
132152
);
153+
exports.Set(
154+
Napi::String::New(env, "askForContactsAccess"), Napi::Function::New(env, AskForContactsAccess)
155+
);
133156

134157
return exports;
135158
}

0 commit comments

Comments
 (0)