|
42 | 42 | return auth_status;
|
43 | 43 | }
|
44 | 44 |
|
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 |
46 | 46 | std::string EventAuthStatus(std::string type) {
|
47 | 47 | std::string auth_status = "Not Determined";
|
48 | 48 |
|
|
125 | 125 | return Napi::Value::From(env, auth_status);
|
126 | 126 | }
|
127 | 127 |
|
| 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 | + |
128 | 148 | // Initializes all functions exposed to JS
|
129 | 149 | Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
130 | 150 | exports.Set(
|
131 | 151 | Napi::String::New(env, "getAuthStatus"), Napi::Function::New(env, GetAuthStatus)
|
132 | 152 | );
|
| 153 | + exports.Set( |
| 154 | + Napi::String::New(env, "askForContactsAccess"), Napi::Function::New(env, AskForContactsAccess) |
| 155 | + ); |
133 | 156 |
|
134 | 157 | return exports;
|
135 | 158 | }
|
|
0 commit comments