Keep a service running on background mode in iOS.
Created
October 25, 2016 06:12
-
-
Save pokk/3fc848456dfa59daae03fdbb3a1d8fe3 to your computer and use it in GitHub Desktop.
Create a service running on background mode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
class BackgroundTaskManager { | |
static let getInstance: BackgroundTaskManager = BackgroundTaskManager() | |
private var masterTaskId: UIBackgroundTaskIdentifier | |
private var bgTaskIdList: [UIBackgroundTaskIdentifier] | |
private init() { | |
self.masterTaskId = UIBackgroundTaskInvalid | |
self.bgTaskIdList = [] | |
} | |
func beginNewBackgroundTask() -> UIBackgroundTaskIdentifier { | |
let application: UIApplication = UIApplication.sharedApplication() | |
var bgTaskId: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid | |
if application.respondsToSelector(#selector(UIApplication.beginBackgroundTaskWithExpirationHandler(_:))) { | |
bgTaskId = application.beginBackgroundTaskWithExpirationHandler({ | |
print("background task \(bgTaskId) expired...") | |
}) | |
if self.masterTaskId == UIBackgroundTaskInvalid { | |
self.masterTaskId = bgTaskId | |
print("started master task \(self.masterTaskId)") | |
} | |
else { | |
print("started background task \(bgTaskId)") | |
self.bgTaskIdList.append(bgTaskId) | |
self.endBackgroundTasks() | |
} | |
} | |
return bgTaskId | |
} | |
@objc func endBackgroundTasks() -> Void { | |
self.drainBGTaskList(false) | |
} | |
func endAllBackgroundTasks() -> Void { | |
self.drainBGTaskList(true) | |
} | |
func drainBGTaskList(all: Bool) -> Void { | |
let application: UIApplication = UIApplication.sharedApplication() | |
let action = #selector(BackgroundTaskManager.endBackgroundTasks) | |
if application.respondsToSelector(action) { | |
for i in (0 ... self.bgTaskIdList.count) { | |
let bgTaskId: UIBackgroundTaskIdentifier = self.bgTaskIdList[i] | |
print("ending background task with id -\(bgTaskId)") | |
application.endBackgroundTask(bgTaskId) | |
self.bgTaskIdList.removeAtIndex(0) | |
} | |
if self.bgTaskIdList.count > 0 { | |
print("kept background task id \(self.bgTaskIdList[0])") | |
} | |
if all { | |
print("no more background tasks running...") | |
application.endBackgroundTask(self.masterTaskId) | |
self.masterTaskId = UIBackgroundTaskInvalid | |
} | |
else { | |
print("kept background task id \(self.masterTaskId)") | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
init{ | |
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(applicationEnterBackground), name: UIApplicationDidEnterBackgroundNotification, object: nil) | |
} | |
} | |
@objc private func applicationEnterBackground() { | |
// Use the BackgroundTaskManager to manage all the background Task | |
let sharedTask: sharedBackgroundTask = sharedBackgroundTask.getInstance | |
sharedTask.bgTask = BackgroundTaskManager.getInstance | |
sharedTask.bgTask?.beginNewBackgroundTask() | |
} | |
private func restartUpdates() { | |
// Restart Location update | |
let sharedTask: sharedBackgroundTask = sharedBackgroundTask.getInstance | |
if nil != sharedTask.timer { | |
sharedTask.timer!.invalidate() | |
sharedTask.timer = nil | |
} | |
} | |
private func stopBackground() { | |
// Stop Background service updating | |
let sharedTask: sharedBackgroundTask = sharedBackgroundTask.getInstance | |
if nil != sharedTask.timer { | |
sharedTask.timer!.invalidate() | |
sharedTask.timer = nil | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment