objc2_contacts/generated/CNContactStore.rs
1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6use objc2_foundation::*;
7
8use crate::*;
9
10/// The entities the user can grant access to.
11///
12/// See also [Apple's documentation](https://developer.apple.com/documentation/contacts/cnentitytype?language=objc)
13// NS_ENUM
14#[repr(transparent)]
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
16pub struct CNEntityType(pub NSInteger);
17impl CNEntityType {
18 /// The user's contacts.
19 #[doc(alias = "CNEntityTypeContacts")]
20 pub const Contacts: Self = Self(0);
21}
22
23unsafe impl Encode for CNEntityType {
24 const ENCODING: Encoding = NSInteger::ENCODING;
25}
26
27unsafe impl RefEncode for CNEntityType {
28 const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
29}
30
31/// The authorization the user has given the application to access an entity type.
32///
33/// See also [Apple's documentation](https://developer.apple.com/documentation/contacts/cnauthorizationstatus?language=objc)
34// NS_ENUM
35#[repr(transparent)]
36#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
37pub struct CNAuthorizationStatus(pub NSInteger);
38impl CNAuthorizationStatus {
39 /// The user has not yet made a choice regarding whether the application may access contact data.
40 #[doc(alias = "CNAuthorizationStatusNotDetermined")]
41 pub const NotDetermined: Self = Self(0);
42 /// The application is not authorized to access contact data.
43 /// The user cannot change this application’s status, possibly due to active restrictions such as parental controls being in place.
44 #[doc(alias = "CNAuthorizationStatusRestricted")]
45 pub const Restricted: Self = Self(1);
46 /// The user explicitly denied access to contact data for the application.
47 #[doc(alias = "CNAuthorizationStatusDenied")]
48 pub const Denied: Self = Self(2);
49 /// The application is authorized to access contact data.
50 #[doc(alias = "CNAuthorizationStatusAuthorized")]
51 pub const Authorized: Self = Self(3);
52 /// This application is authorized to access some contact data.
53 #[doc(alias = "CNAuthorizationStatusLimited")]
54 pub const Limited: Self = Self(4);
55}
56
57unsafe impl Encode for CNAuthorizationStatus {
58 const ENCODING: Encoding = NSInteger::ENCODING;
59}
60
61unsafe impl RefEncode for CNAuthorizationStatus {
62 const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
63}
64
65extern_class!(
66 /// Provides methods to fetch and save contacts.
67 ///
68 ///
69 /// The CNContactStore is a thread safe class that can fetch and save contacts, fetch and save groups, and fetch containers.
70 ///
71 ///
72 /// Note: Some good practices are:
73 /// 1) Only fetch contact properties that will be used.
74 /// 2) When fetching all contacts and caching the results, first fetch all contact identifiers only. Then fetch batches of detailed contacts by identifiers as you need them.
75 /// 3) To aggregate several contact fetches collect a set of unique contact identifiers from the fetches. Then fetch batches of detailed contacts by identifiers.
76 /// 4) When CNContactStoreDidChangeNotification is posted, if you cache any fetched contacts/groups/containers then they must be refetched and the old cached objects released.
77 ///
78 /// See also [Apple's documentation](https://developer.apple.com/documentation/contacts/cncontactstore?language=objc)
79 #[unsafe(super(NSObject))]
80 #[derive(Debug, PartialEq, Eq, Hash)]
81 pub struct CNContactStore;
82);
83
84unsafe impl NSObjectProtocol for CNContactStore {}
85
86impl CNContactStore {
87 extern_methods!(
88 /// Indicates the current authorization status to access contact data.
89 ///
90 ///
91 /// Based upon the access, the application could display or hide its UI elements that would access any Contacts API. This method is thread safe.
92 ///
93 ///
94 /// Returns: Returns the authorization status for the given entityType.
95 #[unsafe(method(authorizationStatusForEntityType:))]
96 #[unsafe(method_family = none)]
97 pub unsafe fn authorizationStatusForEntityType(
98 entity_type: CNEntityType,
99 ) -> CNAuthorizationStatus;
100
101 #[cfg(feature = "block2")]
102 /// Request access to the user's contacts.
103 ///
104 ///
105 /// Users are able to grant or deny access to contact data on a per-application basis. To request access to contact data, call requestAccessForEntityType:completionHandler:. This will not block the application while the user is being asked to grant or deny access. The user will only be prompted the first time access is requested; any subsequent CNContactStore calls will use the existing permissions. The completion handler is called on an arbitrary queue.
106 ///
107 ///
108 /// Note: Recommended to use CNContactStore instance methods in this completion handler instead of the UI main thread. This method is optional when CNContactStore is used on a background thread. If it is not used in that case, CNContactStore will block if the user is asked to grant or deny access.
109 ///
110 ///
111 /// Parameter `entityType`: Set to CNEntityTypeContacts.
112 ///
113 /// Parameter `completionHandler`: This block is called upon completion. If the user grants access then granted is YES and error is nil. Otherwise granted is NO with an error.
114 #[unsafe(method(requestAccessForEntityType:completionHandler:))]
115 #[unsafe(method_family = none)]
116 pub unsafe fn requestAccessForEntityType_completionHandler(
117 &self,
118 entity_type: CNEntityType,
119 completion_handler: &block2::Block<dyn Fn(Bool, *mut NSError)>,
120 );
121
122 #[cfg(feature = "CNContact")]
123 /// Fetch all unified contacts matching a given predicate.
124 ///
125 ///
126 /// Use only predicates from CNContact+Predicates.h. Compound predicates are not supported. Due to unification the returned contacts may have a different identifier.
127 ///
128 ///
129 /// Note: To fetch all contacts use enumerateContactsWithFetchRequest:error:usingBlock:.
130 ///
131 ///
132 /// Parameter `predicate`: The predicate to match against.
133 ///
134 /// Parameter `keys`: The properties to fetch into the returned CNContact objects. Should only fetch the properties that will be used. Can combine contact keys and contact key descriptors.
135 ///
136 /// Parameter `error`: If an error occurs, contains error information.
137 ///
138 /// Returns: An array of CNContact objects matching the predicate. If no matches are found, an empty array is returned. If an error occurs, nil is returned.
139 #[unsafe(method(unifiedContactsMatchingPredicate:keysToFetch:error:_))]
140 #[unsafe(method_family = none)]
141 pub unsafe fn unifiedContactsMatchingPredicate_keysToFetch_error(
142 &self,
143 predicate: &NSPredicate,
144 keys: &NSArray<ProtocolObject<dyn CNKeyDescriptor>>,
145 ) -> Result<Retained<NSArray<CNContact>>, Retained<NSError>>;
146
147 #[cfg(feature = "CNContact")]
148 /// Fetch a unified contact with a given identifier.
149 ///
150 ///
151 /// Due to unification the returned contact may have a different identifier. To fetch a batch of contacts by identifiers use [CNContact predicateForContactsWithIdentifiers:].
152 ///
153 ///
154 /// Parameter `identifier`: The identifier of the contact to fetch.
155 ///
156 /// Parameter `keys`: The properties to fetch into the returned CNContact object. Should only fetch the properties that will be used. Can combine contact keys and contact key descriptors.
157 ///
158 /// Parameter `error`: If an error occurs, contains error information.
159 ///
160 /// Returns: The unified contact matching or linked to the identifier. If no contact with the given identifier is found, nil is returned and error is set to CNErrorCodeRecordDoesNotExist.
161 #[unsafe(method(unifiedContactWithIdentifier:keysToFetch:error:_))]
162 #[unsafe(method_family = none)]
163 pub unsafe fn unifiedContactWithIdentifier_keysToFetch_error(
164 &self,
165 identifier: &NSString,
166 keys: &NSArray<ProtocolObject<dyn CNKeyDescriptor>>,
167 ) -> Result<Retained<CNContact>, Retained<NSError>>;
168
169 #[cfg(feature = "CNContact")]
170 /// Fetch the unified contact that is the "me" card.
171 ///
172 ///
173 /// Fetches the contact that is represented in the user interface as "My Card".
174 ///
175 ///
176 /// Parameter `keys`: The properties to fetch into the returned CNContact object. Should only fetch the properties that will be used. Can combine contact keys and contact key descriptors.
177 ///
178 /// Parameter `error`: If an error occurs, contains error information.
179 ///
180 /// Returns: The unified contact that is the "me" card. If no "me" card is set, nil is returned.
181 #[unsafe(method(unifiedMeContactWithKeysToFetch:error:_))]
182 #[unsafe(method_family = none)]
183 pub unsafe fn unifiedMeContactWithKeysToFetch_error(
184 &self,
185 keys: &NSArray<ProtocolObject<dyn CNKeyDescriptor>>,
186 ) -> Result<Retained<CNContact>, Retained<NSError>>;
187
188 #[cfg(all(
189 feature = "CNContact",
190 feature = "CNContactFetchRequest",
191 feature = "CNFetchRequest",
192 feature = "CNFetchResult"
193 ))]
194 /// Enumerate a contact fetch request.
195 ///
196 ///
197 /// Executes the given fetch request and returns an enumerator for the results.
198 /// This may prevent all records from being loaded into memory at once.
199 ///
200 /// An exception may be thrown if an error occurs during enumeration.
201 ///
202 ///
203 /// Parameter `request`: A description of the records to fetch.
204 ///
205 ///
206 /// Parameter `error`: If the fetch fails, contains an
207 /// `NSError`object with more information.
208 ///
209 ///
210 /// Returns: An enumerator of the records matching the result, or
211 /// `nil`if there was an error.
212 #[unsafe(method(enumeratorForContactFetchRequest:error:_))]
213 #[unsafe(method_family = none)]
214 pub unsafe fn enumeratorForContactFetchRequest_error(
215 &self,
216 request: &CNContactFetchRequest,
217 ) -> Result<Retained<CNFetchResult<NSEnumerator<CNContact>>>, Retained<NSError>>;
218
219 #[cfg(all(
220 feature = "CNChangeHistoryEvent",
221 feature = "CNChangeHistoryFetchRequest",
222 feature = "CNFetchRequest",
223 feature = "CNFetchResult"
224 ))]
225 /// Enumerate a change history fetch request.
226 ///
227 ///
228 /// Executes the given fetch request and returns an enumerator for the results.
229 /// This may prevent all events from being loaded into memory at once.
230 ///
231 /// An exception may be thrown if an error occurs during enumeration.
232 ///
233 ///
234 /// Parameter `request`: A description of the events to fetch.
235 ///
236 ///
237 /// Parameter `error`: If the fetch fails, contains an
238 /// `NSError`object with more information.
239 ///
240 ///
241 /// Returns: An enumerator of the events matching the result, or
242 /// `nil`if there was an error.
243 #[unsafe(method(enumeratorForChangeHistoryFetchRequest:error:_))]
244 #[unsafe(method_family = none)]
245 pub unsafe fn enumeratorForChangeHistoryFetchRequest_error(
246 &self,
247 request: &CNChangeHistoryFetchRequest,
248 ) -> Result<Retained<CNFetchResult<NSEnumerator<CNChangeHistoryEvent>>>, Retained<NSError>>;
249
250 #[cfg(all(
251 feature = "CNContact",
252 feature = "CNContactFetchRequest",
253 feature = "CNFetchRequest",
254 feature = "block2"
255 ))]
256 /// Enumerates all contacts matching a contact fetch request.
257 ///
258 ///
259 /// This method will wait until the enumeration is finished. If there are no results, the block is not called and YES is returned.
260 ///
261 ///
262 /// Parameter `fetchRequest`: The contact fetch request that specifies the search criteria.
263 ///
264 /// Parameter `error`: If an error occurs, contains error information.
265 ///
266 /// Parameter `block`: Called for each matching contact. Set *stop to YES to stop the enumeration.
267 ///
268 /// Returns: YES if successful, otherwise NO.
269 #[unsafe(method(enumerateContactsWithFetchRequest:error:usingBlock:))]
270 #[unsafe(method_family = none)]
271 pub unsafe fn enumerateContactsWithFetchRequest_error_usingBlock(
272 &self,
273 fetch_request: &CNContactFetchRequest,
274 error: Option<&mut Option<Retained<NSError>>>,
275 block: &block2::Block<dyn Fn(NonNull<CNContact>, NonNull<Bool>) + '_>,
276 ) -> bool;
277
278 #[cfg(feature = "CNGroup")]
279 /// Fetch all groups matching a given predicate.
280 ///
281 ///
282 /// Use only predicates from CNGroup+Predicates.h. Compound predicates are not supported.
283 ///
284 ///
285 /// Parameter `predicate`: The predicate to match against. Set to nil to match all groups.
286 ///
287 /// Parameter `error`: If an error occurs, contains error information.
288 ///
289 /// Returns: An array of CNGroup objects matching the predicate. If no matches are found, an empty array is returned. If an error occurs, nil is returned.
290 #[unsafe(method(groupsMatchingPredicate:error:_))]
291 #[unsafe(method_family = none)]
292 pub unsafe fn groupsMatchingPredicate_error(
293 &self,
294 predicate: Option<&NSPredicate>,
295 ) -> Result<Retained<NSArray<CNGroup>>, Retained<NSError>>;
296
297 #[cfg(feature = "CNContainer")]
298 /// Fetch all containers matching a given predicate.
299 ///
300 ///
301 /// Use only predicates from CNContainer+Predicates.h. Compound predicates are not supported.
302 ///
303 ///
304 /// Parameter `predicate`: The predicate to match against. Set to nil to match all containers.
305 ///
306 /// Parameter `error`: If an error occurs, contains error information.
307 ///
308 /// Returns: An array of CNContainer objects matching the predicate. If no matches are found, an empty array is returned. If an error occurs, nil is returned.
309 #[unsafe(method(containersMatchingPredicate:error:_))]
310 #[unsafe(method_family = none)]
311 pub unsafe fn containersMatchingPredicate_error(
312 &self,
313 predicate: Option<&NSPredicate>,
314 ) -> Result<Retained<NSArray<CNContainer>>, Retained<NSError>>;
315
316 #[cfg(feature = "CNSaveRequest")]
317 /// Executes a save request.
318 ///
319 ///
320 /// Do not access objects when save request is executing. A save request with contacts may modify the contacts while executing. A save request only applies the changes to the objects. If there are overlapping changes with multiple, concurrent CNSaveRequests then the last saved change wins.
321 ///
322 ///
323 /// Parameter `saveRequest`: Save request to execute.
324 ///
325 /// Parameter `error`: If an error occurs, contains error information.
326 ///
327 /// Returns: YES if successful, otherwise NO.
328 #[unsafe(method(executeSaveRequest:error:_))]
329 #[unsafe(method_family = none)]
330 pub unsafe fn executeSaveRequest_error(
331 &self,
332 save_request: &CNSaveRequest,
333 ) -> Result<(), Retained<NSError>>;
334
335 /// The current history token.
336 ///
337 ///
338 /// Retrieve the current history token. If you are fetching contacts or change history events, you should use the token on the
339 /// `CNFetchResult`instead.
340 #[unsafe(method(currentHistoryToken))]
341 #[unsafe(method_family = none)]
342 pub unsafe fn currentHistoryToken(&self) -> Option<Retained<NSData>>;
343
344 /// The identifier of the default container.
345 ///
346 ///
347 /// This identifier can be used to fetch the default container.
348 ///
349 ///
350 /// Returns: The identifier of the default container. If the caller lacks Contacts authorization or an error occurs, nil is returned.
351 #[unsafe(method(defaultContainerIdentifier))]
352 #[unsafe(method_family = none)]
353 pub unsafe fn defaultContainerIdentifier(&self) -> Retained<NSString>;
354 );
355}
356
357/// Methods declared on superclass `NSObject`.
358impl CNContactStore {
359 extern_methods!(
360 #[unsafe(method(init))]
361 #[unsafe(method_family = init)]
362 pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
363
364 #[unsafe(method(new))]
365 #[unsafe(method_family = new)]
366 pub unsafe fn new() -> Retained<Self>;
367 );
368}
369
370extern "C" {
371 /// Notification posted when changes occur in another CNContactStore.
372 ///
373 /// See also [Apple's documentation](https://developer.apple.com/documentation/contacts/cncontactstoredidchangenotification?language=objc)
374 pub static CNContactStoreDidChangeNotification: &'static NSString;
375}