rust_macios/background_tasks/
bg_task.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{object, 
4    foundation::NSString,
5    objective_c_runtime::{
6        macros::{interface_impl},
7        traits::{FromId, PNSObject},
8    },
9};
10
11object! {
12    /// An abstract class representing a task that’s run while the app is in the background.
13    unsafe pub struct BGTask;
14}
15
16#[interface_impl(NSObject)]
17impl BGTask {
18    /// The identifier of the task.
19    #[property]
20    pub fn identifier(&self) -> NSString {
21        unsafe { NSString::from_id(msg_send![self.m_self(), identifier]) }
22    }
23
24    /// A handler called shortly before the task’s background time expires.
25    #[property]
26    pub fn expiration_handler(&self) {
27        unsafe { msg_send![self.m_self(), expirationHandler] }
28    }
29
30    /// Informs the background task scheduler that the task is complete.
31    ///
32    /// # Arguments
33    ///
34    /// * success - A [`bool`] indicating if the task completed successfully or not.
35    #[method]
36    pub fn set_task_completed_with_success(&mut self, success: bool) {
37        unsafe { msg_send![self.m_self(), setTaskCompletedWithSuccess: success] }
38    }
39}
40
41impl Default for BGTask {
42    fn default() -> Self {
43        Self::m_new()
44    }
45}