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
84extern_conformance!(
85    unsafe impl NSObjectProtocol for CNContactStore {}
86);
87
88impl CNContactStore {
89    extern_methods!(
90        /// Indicates the current authorization status to access contact data.
91        ///
92        ///
93        /// Based upon the access, the application could display or hide its UI elements that would access any Contacts API. This method is thread safe.
94        ///
95        ///
96        /// Returns: Returns the authorization status for the given entityType.
97        #[unsafe(method(authorizationStatusForEntityType:))]
98        #[unsafe(method_family = none)]
99        pub unsafe fn authorizationStatusForEntityType(
100            entity_type: CNEntityType,
101        ) -> CNAuthorizationStatus;
102
103        #[cfg(feature = "block2")]
104        /// Request access to the user's contacts.
105        ///
106        ///
107        /// 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.
108        ///
109        ///
110        /// 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.
111        ///
112        ///
113        /// Parameter `entityType`: Set to CNEntityTypeContacts.
114        ///
115        /// 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.
116        #[unsafe(method(requestAccessForEntityType:completionHandler:))]
117        #[unsafe(method_family = none)]
118        pub unsafe fn requestAccessForEntityType_completionHandler(
119            &self,
120            entity_type: CNEntityType,
121            completion_handler: &block2::DynBlock<dyn Fn(Bool, *mut NSError)>,
122        );
123
124        #[cfg(feature = "CNContact")]
125        /// Fetch all unified contacts matching a given predicate.
126        ///
127        ///
128        /// Use only predicates from CNContact+Predicates.h. Compound predicates are not supported. Due to unification the returned contacts may have a different identifier.
129        ///
130        ///
131        /// Note: To fetch all contacts use enumerateContactsWithFetchRequest:error:usingBlock:.
132        ///
133        ///
134        /// Parameter `predicate`: The predicate to match against.
135        ///
136        /// 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.
137        ///
138        /// Parameter `error`: If an error occurs, contains error information.
139        ///
140        /// 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.
141        #[unsafe(method(unifiedContactsMatchingPredicate:keysToFetch:error:_))]
142        #[unsafe(method_family = none)]
143        pub unsafe fn unifiedContactsMatchingPredicate_keysToFetch_error(
144            &self,
145            predicate: &NSPredicate,
146            keys: &NSArray<ProtocolObject<dyn CNKeyDescriptor>>,
147        ) -> Result<Retained<NSArray<CNContact>>, Retained<NSError>>;
148
149        #[cfg(feature = "CNContact")]
150        /// Fetch a unified contact with a given identifier.
151        ///
152        ///
153        /// Due to unification the returned contact may have a different identifier. To fetch a batch of contacts by identifiers use [CNContact predicateForContactsWithIdentifiers:].
154        ///
155        ///
156        /// Parameter `identifier`: The identifier of the contact to fetch.
157        ///
158        /// 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.
159        ///
160        /// Parameter `error`: If an error occurs, contains error information.
161        ///
162        /// 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.
163        #[unsafe(method(unifiedContactWithIdentifier:keysToFetch:error:_))]
164        #[unsafe(method_family = none)]
165        pub unsafe fn unifiedContactWithIdentifier_keysToFetch_error(
166            &self,
167            identifier: &NSString,
168            keys: &NSArray<ProtocolObject<dyn CNKeyDescriptor>>,
169        ) -> Result<Retained<CNContact>, Retained<NSError>>;
170
171        #[cfg(feature = "CNContact")]
172        /// Fetch the unified contact that is the "me" card.
173        ///
174        ///
175        /// Fetches the contact that is represented in the user interface as "My Card".
176        ///
177        ///
178        /// 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.
179        ///
180        /// Parameter `error`: If an error occurs, contains error information.
181        ///
182        /// Returns: The unified contact that is the "me" card. If no "me" card is set, nil is returned.
183        #[unsafe(method(unifiedMeContactWithKeysToFetch:error:_))]
184        #[unsafe(method_family = none)]
185        pub unsafe fn unifiedMeContactWithKeysToFetch_error(
186            &self,
187            keys: &NSArray<ProtocolObject<dyn CNKeyDescriptor>>,
188        ) -> Result<Retained<CNContact>, Retained<NSError>>;
189
190        #[cfg(all(
191            feature = "CNContact",
192            feature = "CNContactFetchRequest",
193            feature = "CNFetchRequest",
194            feature = "CNFetchResult"
195        ))]
196        /// Enumerate a contact fetch request.
197        ///
198        ///
199        /// Executes the given fetch request and returns an enumerator for the results.
200        /// This may prevent all records from being loaded into memory at once.
201        ///
202        /// An exception may be thrown if an error occurs during enumeration.
203        ///
204        ///
205        /// Parameter `request`: A description of the records to fetch.
206        ///
207        ///
208        /// Parameter `error`: If the fetch fails, contains an
209        /// `NSError`object with more information.
210        ///
211        ///
212        /// Returns: An enumerator of the records matching the result, or
213        /// `nil`if there was an error.
214        ///
215        /// # Safety
216        ///
217        /// The returned generic enumerator's underlying collection should not be mutated while in use.
218        #[unsafe(method(enumeratorForContactFetchRequest:error:_))]
219        #[unsafe(method_family = none)]
220        pub unsafe fn enumeratorForContactFetchRequest_error(
221            &self,
222            request: &CNContactFetchRequest,
223        ) -> Result<Retained<CNFetchResult<NSEnumerator<CNContact>>>, Retained<NSError>>;
224
225        #[cfg(all(
226            feature = "CNChangeHistoryEvent",
227            feature = "CNChangeHistoryFetchRequest",
228            feature = "CNFetchRequest",
229            feature = "CNFetchResult"
230        ))]
231        /// Enumerate a change history fetch request.
232        ///
233        ///
234        /// Executes the given fetch request and returns an enumerator for the results.
235        /// This may prevent all events from being loaded into memory at once.
236        ///
237        /// An exception may be thrown if an error occurs during enumeration.
238        ///
239        ///
240        /// Parameter `request`: A description of the events to fetch.
241        ///
242        ///
243        /// Parameter `error`: If the fetch fails, contains an
244        /// `NSError`object with more information.
245        ///
246        ///
247        /// Returns: An enumerator of the events matching the result, or
248        /// `nil`if there was an error.
249        ///
250        /// # Safety
251        ///
252        /// The returned generic enumerator's underlying collection should not be mutated while in use.
253        #[unsafe(method(enumeratorForChangeHistoryFetchRequest:error:_))]
254        #[unsafe(method_family = none)]
255        pub unsafe fn enumeratorForChangeHistoryFetchRequest_error(
256            &self,
257            request: &CNChangeHistoryFetchRequest,
258        ) -> Result<Retained<CNFetchResult<NSEnumerator<CNChangeHistoryEvent>>>, Retained<NSError>>;
259
260        #[cfg(all(
261            feature = "CNContact",
262            feature = "CNContactFetchRequest",
263            feature = "CNFetchRequest",
264            feature = "block2"
265        ))]
266        /// Enumerates all contacts matching a contact fetch request.
267        ///
268        ///
269        /// This method will wait until the enumeration is finished. If there are no results, the block is not called and YES is returned.
270        ///
271        ///
272        /// Parameter `fetchRequest`: The contact fetch request that specifies the search criteria.
273        ///
274        /// Parameter `error`: If an error occurs, contains error information.
275        ///
276        /// Parameter `block`: Called for each matching contact. Set *stop to YES to stop the enumeration.
277        ///
278        /// Returns: YES if successful, otherwise NO.
279        #[unsafe(method(enumerateContactsWithFetchRequest:error:usingBlock:))]
280        #[unsafe(method_family = none)]
281        pub unsafe fn enumerateContactsWithFetchRequest_error_usingBlock(
282            &self,
283            fetch_request: &CNContactFetchRequest,
284            error: Option<&mut Option<Retained<NSError>>>,
285            block: &block2::DynBlock<dyn Fn(NonNull<CNContact>, NonNull<Bool>) + '_>,
286        ) -> bool;
287
288        #[cfg(feature = "CNGroup")]
289        /// Fetch all groups matching a given predicate.
290        ///
291        ///
292        /// Use only predicates from CNGroup+Predicates.h. Compound predicates are not supported.
293        ///
294        ///
295        /// Parameter `predicate`: The predicate to match against. Set to nil to match all groups.
296        ///
297        /// Parameter `error`: If an error occurs, contains error information.
298        ///
299        /// 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.
300        #[unsafe(method(groupsMatchingPredicate:error:_))]
301        #[unsafe(method_family = none)]
302        pub unsafe fn groupsMatchingPredicate_error(
303            &self,
304            predicate: Option<&NSPredicate>,
305        ) -> Result<Retained<NSArray<CNGroup>>, Retained<NSError>>;
306
307        #[cfg(feature = "CNContainer")]
308        /// Fetch all containers matching a given predicate.
309        ///
310        ///
311        /// Use only predicates from CNContainer+Predicates.h. Compound predicates are not supported.
312        ///
313        ///
314        /// Parameter `predicate`: The predicate to match against. Set to nil to match all containers.
315        ///
316        /// Parameter `error`: If an error occurs, contains error information.
317        ///
318        /// 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.
319        #[unsafe(method(containersMatchingPredicate:error:_))]
320        #[unsafe(method_family = none)]
321        pub unsafe fn containersMatchingPredicate_error(
322            &self,
323            predicate: Option<&NSPredicate>,
324        ) -> Result<Retained<NSArray<CNContainer>>, Retained<NSError>>;
325
326        #[cfg(feature = "CNSaveRequest")]
327        /// Executes a save request.
328        ///
329        ///
330        /// 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.
331        ///
332        ///
333        /// Parameter `saveRequest`: Save request to execute.
334        ///
335        /// Parameter `error`: If an error occurs, contains error information.
336        ///
337        /// Returns: YES if successful, otherwise NO.
338        #[unsafe(method(executeSaveRequest:error:_))]
339        #[unsafe(method_family = none)]
340        pub unsafe fn executeSaveRequest_error(
341            &self,
342            save_request: &CNSaveRequest,
343        ) -> Result<(), Retained<NSError>>;
344
345        /// The current history token.
346        ///
347        ///
348        /// Retrieve the current history token. If you are fetching contacts or change history events, you should use the token on the
349        /// `CNFetchResult`instead.
350        #[unsafe(method(currentHistoryToken))]
351        #[unsafe(method_family = none)]
352        pub unsafe fn currentHistoryToken(&self) -> Option<Retained<NSData>>;
353
354        /// The identifier of the default container.
355        ///
356        ///
357        /// This identifier can be used to fetch the default container.
358        ///
359        ///
360        /// Returns: The identifier of the default container. If the caller lacks Contacts authorization or an error occurs, nil is returned.
361        #[unsafe(method(defaultContainerIdentifier))]
362        #[unsafe(method_family = none)]
363        pub unsafe fn defaultContainerIdentifier(&self) -> Retained<NSString>;
364    );
365}
366
367/// Methods declared on superclass `NSObject`.
368impl CNContactStore {
369    extern_methods!(
370        #[unsafe(method(init))]
371        #[unsafe(method_family = init)]
372        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
373
374        #[unsafe(method(new))]
375        #[unsafe(method_family = new)]
376        pub unsafe fn new() -> Retained<Self>;
377    );
378}
379
380extern "C" {
381    /// Notification posted when changes occur in another CNContactStore.
382    ///
383    /// See also [Apple's documentation](https://developer.apple.com/documentation/contacts/cncontactstoredidchangenotification?language=objc)
384    pub static CNContactStoreDidChangeNotification: &'static NSString;
385}