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        #[unsafe(method(enumeratorForContactFetchRequest:error:_))]
215        #[unsafe(method_family = none)]
216        pub unsafe fn enumeratorForContactFetchRequest_error(
217            &self,
218            request: &CNContactFetchRequest,
219        ) -> Result<Retained<CNFetchResult<NSEnumerator<CNContact>>>, Retained<NSError>>;
220
221        #[cfg(all(
222            feature = "CNChangeHistoryEvent",
223            feature = "CNChangeHistoryFetchRequest",
224            feature = "CNFetchRequest",
225            feature = "CNFetchResult"
226        ))]
227        /// Enumerate a change history fetch request.
228        ///
229        ///
230        /// Executes the given fetch request and returns an enumerator for the results.
231        /// This may prevent all events from being loaded into memory at once.
232        ///
233        /// An exception may be thrown if an error occurs during enumeration.
234        ///
235        ///
236        /// Parameter `request`: A description of the events to fetch.
237        ///
238        ///
239        /// Parameter `error`: If the fetch fails, contains an
240        /// `NSError`object with more information.
241        ///
242        ///
243        /// Returns: An enumerator of the events matching the result, or
244        /// `nil`if there was an error.
245        #[unsafe(method(enumeratorForChangeHistoryFetchRequest:error:_))]
246        #[unsafe(method_family = none)]
247        pub unsafe fn enumeratorForChangeHistoryFetchRequest_error(
248            &self,
249            request: &CNChangeHistoryFetchRequest,
250        ) -> Result<Retained<CNFetchResult<NSEnumerator<CNChangeHistoryEvent>>>, Retained<NSError>>;
251
252        #[cfg(all(
253            feature = "CNContact",
254            feature = "CNContactFetchRequest",
255            feature = "CNFetchRequest",
256            feature = "block2"
257        ))]
258        /// Enumerates all contacts matching a contact fetch request.
259        ///
260        ///
261        /// This method will wait until the enumeration is finished. If there are no results, the block is not called and YES is returned.
262        ///
263        ///
264        /// Parameter `fetchRequest`: The contact fetch request that specifies the search criteria.
265        ///
266        /// Parameter `error`: If an error occurs, contains error information.
267        ///
268        /// Parameter `block`: Called for each matching contact. Set *stop to YES to stop the enumeration.
269        ///
270        /// Returns: YES if successful, otherwise NO.
271        #[unsafe(method(enumerateContactsWithFetchRequest:error:usingBlock:))]
272        #[unsafe(method_family = none)]
273        pub unsafe fn enumerateContactsWithFetchRequest_error_usingBlock(
274            &self,
275            fetch_request: &CNContactFetchRequest,
276            error: Option<&mut Option<Retained<NSError>>>,
277            block: &block2::DynBlock<dyn Fn(NonNull<CNContact>, NonNull<Bool>) + '_>,
278        ) -> bool;
279
280        #[cfg(feature = "CNGroup")]
281        /// Fetch all groups matching a given predicate.
282        ///
283        ///
284        /// Use only predicates from CNGroup+Predicates.h. Compound predicates are not supported.
285        ///
286        ///
287        /// Parameter `predicate`: The predicate to match against. Set to nil to match all groups.
288        ///
289        /// Parameter `error`: If an error occurs, contains error information.
290        ///
291        /// 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.
292        #[unsafe(method(groupsMatchingPredicate:error:_))]
293        #[unsafe(method_family = none)]
294        pub unsafe fn groupsMatchingPredicate_error(
295            &self,
296            predicate: Option<&NSPredicate>,
297        ) -> Result<Retained<NSArray<CNGroup>>, Retained<NSError>>;
298
299        #[cfg(feature = "CNContainer")]
300        /// Fetch all containers matching a given predicate.
301        ///
302        ///
303        /// Use only predicates from CNContainer+Predicates.h. Compound predicates are not supported.
304        ///
305        ///
306        /// Parameter `predicate`: The predicate to match against. Set to nil to match all containers.
307        ///
308        /// Parameter `error`: If an error occurs, contains error information.
309        ///
310        /// 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.
311        #[unsafe(method(containersMatchingPredicate:error:_))]
312        #[unsafe(method_family = none)]
313        pub unsafe fn containersMatchingPredicate_error(
314            &self,
315            predicate: Option<&NSPredicate>,
316        ) -> Result<Retained<NSArray<CNContainer>>, Retained<NSError>>;
317
318        #[cfg(feature = "CNSaveRequest")]
319        /// Executes a save request.
320        ///
321        ///
322        /// 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.
323        ///
324        ///
325        /// Parameter `saveRequest`: Save request to execute.
326        ///
327        /// Parameter `error`: If an error occurs, contains error information.
328        ///
329        /// Returns: YES if successful, otherwise NO.
330        #[unsafe(method(executeSaveRequest:error:_))]
331        #[unsafe(method_family = none)]
332        pub unsafe fn executeSaveRequest_error(
333            &self,
334            save_request: &CNSaveRequest,
335        ) -> Result<(), Retained<NSError>>;
336
337        /// The current history token.
338        ///
339        ///
340        /// Retrieve the current history token. If you are fetching contacts or change history events, you should use the token on the
341        /// `CNFetchResult`instead.
342        #[unsafe(method(currentHistoryToken))]
343        #[unsafe(method_family = none)]
344        pub unsafe fn currentHistoryToken(&self) -> Option<Retained<NSData>>;
345
346        /// The identifier of the default container.
347        ///
348        ///
349        /// This identifier can be used to fetch the default container.
350        ///
351        ///
352        /// Returns: The identifier of the default container. If the caller lacks Contacts authorization or an error occurs, nil is returned.
353        #[unsafe(method(defaultContainerIdentifier))]
354        #[unsafe(method_family = none)]
355        pub unsafe fn defaultContainerIdentifier(&self) -> Retained<NSString>;
356    );
357}
358
359/// Methods declared on superclass `NSObject`.
360impl CNContactStore {
361    extern_methods!(
362        #[unsafe(method(init))]
363        #[unsafe(method_family = init)]
364        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
365
366        #[unsafe(method(new))]
367        #[unsafe(method_family = new)]
368        pub unsafe fn new() -> Retained<Self>;
369    );
370}
371
372extern "C" {
373    /// Notification posted when changes occur in another CNContactStore.
374    ///
375    /// See also [Apple's documentation](https://developer.apple.com/documentation/contacts/cncontactstoredidchangenotification?language=objc)
376    pub static CNContactStoreDidChangeNotification: &'static NSString;
377}