I am an iOS developer and I have developed an app that runs a periodic background task using Background Modes, I want to implement the same feature in the MacOS version of this app, as I am new to the MacOS development, I am facing some issue regarding implementation of this feature.
I have used NSBackgroundActivityScheduler to schedule a periodic activity, which works fine, when the app is running or is in the dock. But I failed to achieve the same result , when I quit the application. I want to run that task even after the application terminates or quits.
func startBackgroundTask() {
let activity = NSBackgroundActivityScheduler.init(identifier:"com.abc.xyz")
activity.invalidate()
activity.interval = 60
activity.repeats = true
activity.qualityOfService = QualityOfService.background
activity.schedule() {(completion: NSBackgroundActivityScheduler.CompletionHandler) in
print("background task scheduled")
// Perform the activity
NotificationManager.sharedManager().showNotification(informativeText: "Next notification will come after 1 minute") //This is to find notify the background service started.
//perform operation over here...
completion(NSBackgroundActivityScheduler.Result.finished)
}
I have researched various options regarding this , like XPC , LaunchAgent , LoginItems , Process etc. but I am confused which option to go with. As on apple developer forum they have used XPC with LoginItems, but I got stuck when I tried to implement it.