objc2_background_tasks/generated/BGTaskScheduler.rs
1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5#[cfg(feature = "dispatch2")]
6use dispatch2::*;
7use objc2::__framework_prelude::*;
8use objc2_foundation::*;
9
10use crate::*;
11
12extern "C" {
13 /// The background tasks error domain as a string.
14 ///
15 /// See also [Apple's documentation](https://developer.apple.com/documentation/backgroundtasks/bgtaskschedulererrordomain?language=objc)
16 pub static BGTaskSchedulerErrorDomain: &'static NSErrorDomain;
17}
18
19/// An enumeration of the task scheduling errors.
20///
21/// See also [Apple's documentation](https://developer.apple.com/documentation/backgroundtasks/bgtaskschedulererrorcode?language=objc)
22// NS_ERROR_ENUM
23#[repr(transparent)]
24#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
25pub struct BGTaskSchedulerErrorCode(pub NSInteger);
26impl BGTaskSchedulerErrorCode {
27 /// A task scheduling error indicating that the app or extension can’t schedule background work.
28 ///
29 /// This error usually occurs for one of following reasons:
30 ///
31 /// - The user has disabled background refresh in settings.
32 /// - The app is running on Simulator which doesn’t support background processing.
33 /// - The keyboard extension either hasn’t set
34 /// <doc
35 /// ://com.apple.documentation/documentation/bundleresources/information_property_list/nsextension/nsextensionattributes/requestsopenaccess>
36 /// to `YES` in [The Info.plist File](https://developer.apple.com/library/archive/documentation/Carbon/Conceptual/ProvidingUserAssitAppleHelp/authoring_help/authoring_help_book.html#//apple_ref/doc/uid/TP30000903-CH206-SW22), or the user hasn’t granted open access.
37 /// - The extension type isn’t able to schedule background tasks.
38 #[doc(alias = "BGTaskSchedulerErrorCodeUnavailable")]
39 pub const Unavailable: Self = Self(1);
40 /// A task scheduling error indicating that there are too many pending tasks of the type requested.
41 ///
42 /// Try canceling some existing task requests and then resubmit the request that failed.
43 #[doc(alias = "BGTaskSchedulerErrorCodeTooManyPendingTaskRequests")]
44 pub const TooManyPendingTaskRequests: Self = Self(2);
45 /// A task scheduling error indicating the app isn’t permitted to schedule the task.
46 ///
47 /// There are four causes for this error:
48 ///
49 /// - The app doesn’t set the appropriate mode in the
50 /// <doc
51 /// ://com.apple.documentation/documentation/bundleresources/information_property_list/uibackgroundmodes>
52 /// array.
53 /// - The task identifier of the submitted task wasn’t in the
54 /// <doc
55 /// ://com.apple.documentation/documentation/bundleresources/information_property_list/bgtaskschedulerpermittedidentifiers>
56 /// array in [the Info.plist](https://developer.apple.com/library/archive/documentation/Carbon/Conceptual/ProvidingUserAssitAppleHelp/authoring_help/authoring_help_book.html#//apple_ref/doc/uid/TP30000903-CH206-SW22).
57 /// - The task requested additional ``BGContinuedProcessingTaskRequestResources`` that are unavailable.
58 /// - The user has explicitly denied background launches for your app.
59 #[doc(alias = "BGTaskSchedulerErrorCodeNotPermitted")]
60 pub const NotPermitted: Self = Self(3);
61 /// A ``BGContinuedProcessingTaskRequest`` was not allowed to immediately run due to system conditions.
62 ///
63 /// This will only be returned when using the ``BGContinuedProcessingTaskRequestSubmissionStrategyFail`` when
64 /// submitting a ``BGContinuedProcessingTaskRequest``. Task requests that are successfully ran will not be
65 /// associated with any error code.
66 #[doc(alias = "BGTaskSchedulerErrorCodeImmediateRunIneligible")]
67 pub const ImmediateRunIneligible: Self = Self(4);
68}
69
70unsafe impl Encode for BGTaskSchedulerErrorCode {
71 const ENCODING: Encoding = NSInteger::ENCODING;
72}
73
74unsafe impl RefEncode for BGTaskSchedulerErrorCode {
75 const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
76}
77
78extern_class!(
79 /// A class for scheduling task requests that launch your app in the background.
80 ///
81 /// Background tasks give your app a way to run code while the app is suspended.
82 /// To learn how to register, schedule, and run a background task, see
83 /// <doc
84 /// ://com.apple.documentation/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app>.
85 ///
86 /// See also [Apple's documentation](https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler?language=objc)
87 #[unsafe(super(NSObject))]
88 #[derive(Debug, PartialEq, Eq, Hash)]
89 pub struct BGTaskScheduler;
90);
91
92extern_conformance!(
93 unsafe impl NSObjectProtocol for BGTaskScheduler {}
94);
95
96impl BGTaskScheduler {
97 extern_methods!(
98 #[unsafe(method(init))]
99 #[unsafe(method_family = init)]
100 pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
101
102 #[unsafe(method(new))]
103 #[unsafe(method_family = new)]
104 pub unsafe fn new() -> Retained<Self>;
105
106 /// The shared background task scheduler instance.
107 #[unsafe(method(sharedScheduler))]
108 #[unsafe(method_family = none)]
109 pub unsafe fn sharedScheduler() -> Retained<BGTaskScheduler>;
110
111 #[cfg(feature = "BGTaskRequest")]
112 /// A bitfield of the resources the device supports for ``BackgroundTasks/BGContinuedProcessingTaskRequest`` instances.
113 #[unsafe(method(supportedResources))]
114 #[unsafe(method_family = none)]
115 pub unsafe fn supportedResources() -> BGContinuedProcessingTaskRequestResources;
116
117 #[cfg(all(feature = "BGTask", feature = "block2", feature = "dispatch2"))]
118 /// Register a launch handler for the task with the associated identifier that’s executed on the specified queue.
119 ///
120 /// Every identifier in the
121 /// <doc
122 /// ://com.apple.documentation/documentation/bundleresources/information_property_list/bgtaskschedulerpermittedidentifiers>
123 /// requires a handler. Registration of all launch handlers must be complete before the end of
124 /// <doc
125 /// ://com.apple.documentation/documentation/uikit/uiapplicationdelegate/1623053-applicationdidfinishlaunching>.
126 ///
127 /// You must register launch handlers before your application finishes launching (``BGContinuedProcessingTask``
128 /// registrations are exempt from this requirement). Attempting to register a handler after launch or multiple handlers
129 /// for the same identifier is an error. Although you may submit task requests from some extensions, only the host app
130 /// will be launched to handle background work.
131 ///
132 /// - Parameters:
133 /// - identifier: The identifier for the task that will be handled by the provided launch handler.
134 /// - queue: A queue for executing the task. Pass `nil` to use a default background queue.
135 /// - launchHandler: The system runs the block of code for the launch handler when it launches the app in the background. The block takes a single parameter, a ``BGTask`` object used for assigning an expiration handler and for setting a completion status. The block has no return value. Assign an expiration handler to the task's expirationHandler property and call setTaskCompletedWithSuccess: when the background work is complete.
136 /// - Returns: Returns
137 /// <doc
138 /// ://com.apple.documentation/documentation/objectivec/yes> if the launch handler was registered. Returns
139 /// <doc
140 /// ://com.apple.documentation/documentation/objectivec/no> if the identifier isn't included in the
141 /// <doc
142 /// ://com.apple.documentation/documentation/bundleresources/information_property_list/bgtaskschedulerpermittedidentifiers> `Info.plist`.
143 /// - Important: Register each task identifier only once. The system kills the app on the second registration of the same task identifier.
144 ///
145 /// # Safety
146 ///
147 /// `queue` possibly has additional threading requirements.
148 #[unsafe(method(registerForTaskWithIdentifier:usingQueue:launchHandler:))]
149 #[unsafe(method_family = none)]
150 pub unsafe fn registerForTaskWithIdentifier_usingQueue_launchHandler(
151 &self,
152 identifier: &NSString,
153 queue: Option<&DispatchQueue>,
154 launch_handler: &block2::DynBlock<dyn Fn(NonNull<BGTask>)>,
155 ) -> bool;
156
157 #[cfg(feature = "BGTaskRequest")]
158 /// Submit a previously registered background task for execution.
159 ///
160 /// Submitting a task request for an unexecuted task that’s already in the queue replaces the previous task request.
161 ///
162 /// There can be a total of 1 refresh task and 10 processing tasks scheduled at any time. Trying to schedule more tasks
163 /// returns ``BGTaskSchedulerErrorCode/BGTaskSchedulerErrorCodeTooManyPendingTaskRequests``.
164 ///
165 /// - Parameters:
166 /// - taskRequest: The task request object representing the parameters of the background task to be scheduled.
167 /// - error: If an error occurs, upon return contains an error object that indicates why the request was rejected
168 /// - Returns: `YES` if the request was successfully submitted; `NO` if there was an error
169 #[unsafe(method(submitTaskRequest:error:_))]
170 #[unsafe(method_family = none)]
171 pub unsafe fn submitTaskRequest_error(
172 &self,
173 task_request: &BGTaskRequest,
174 ) -> Result<(), Retained<NSError>>;
175
176 /// Cancel a previously scheduled task request.
177 ///
178 /// - Parameters:
179 /// - identifier: The identifier of the previously submitted task request to cancel.
180 #[unsafe(method(cancelTaskRequestWithIdentifier:))]
181 #[unsafe(method_family = none)]
182 pub unsafe fn cancelTaskRequestWithIdentifier(&self, identifier: &NSString);
183
184 /// Cancel all previously submitted task requests.
185 #[unsafe(method(cancelAllTaskRequests))]
186 #[unsafe(method_family = none)]
187 pub unsafe fn cancelAllTaskRequests(&self);
188
189 #[cfg(all(feature = "BGTaskRequest", feature = "block2"))]
190 /// Returns a list of all task requests that have been submitted but not yet completed.
191 ///
192 /// The handler takes a single parameter `tasksRequests`, an array of `BGTaskRequest`
193 /// objects. The array is empty if there are no scheduled tasks.
194 ///
195 /// The objects passed in the array are copies of the existing requests. Changing the attributes of a request has no
196 /// effect. To change the attributes submit a new task request using ``BGTaskScheduler/submitTaskRequest:error:``.
197 ///
198 /// - Parameters:
199 /// - completionHandler: The completion handler called with the pending tasks.
200 /// - Note: The handler may execute on a background thread.
201 #[unsafe(method(getPendingTaskRequestsWithCompletionHandler:))]
202 #[unsafe(method_family = none)]
203 pub unsafe fn getPendingTaskRequestsWithCompletionHandler(
204 &self,
205 completion_handler: &block2::DynBlock<dyn Fn(NonNull<NSArray<BGTaskRequest>>)>,
206 );
207 );
208}