Swift如何使用NSTimer背景?
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: true) } func update() { println("Something cool") } }
对于模拟器来说没关系,我会通过点击主页按钮获得连续的“Something cool”。 但是当我使用iPhone调试应用程序时,它就解决了。 当我点击主页按钮并让我的应用程序运行背景时,我没有得到任何东西。 有人告诉我,我可以播放一段很长的空白音乐,让我的应用程序在后台运行。但我不知道如何用swift @matt在后台播放音乐
您可以使用beginBackgroundTaskWithExpirationHandler
来获取一些后台执行时间。
class ViewController: UIViewController { var backgroundTaskIdentifier: UIBackgroundTaskIdentifier? override func viewDidLoad() { super.viewDidLoad() backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskIdentifier!) }) var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true) } func update() { println("Something cool") } }
Swift 3.0
backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: { UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier!) })
这个代码的灵感来自于这个答案 ,但我移植到了swift。
这显然只在iOS 7+上运行3分钟。
我愿意打赌你不需要在后台运行计时器,你需要知道你被暂停时已经过去的时间。 这比使用空声音文件更好地利用您的资源。
使用applicationWillResignActive
和applicationDidBecomeActive
确定已经过了多长时间。 首先保存计时器的fireDate,并在applicationWillResignActive
使其无效
func applicationWillResignActive(application: UIApplication) { guard let t = self.timer else { return } nextFireDate = t.fireDate t.invalidate() }
接下来,通过将现在的时间与您在applicationWillResignActive
调用中保存的fireDate进行比较,确定applicationDidBecomeActive
的计时器剩余时间:
func applicationDidBecomeActive(application: UIApplication) { guard let n = nextFireDate else { return } let howMuchLonger = n.timeIntervalSinceDate(NSDate()) if howMuchLonger < 0 { print("Should have already fired \(howMuchLonger) seconds ago") target!.performSelector(selector!) } else { print("should fire in \(howMuchLonger) seconds") NSTimer.scheduledTimerWithTimeInterval(howMuchLonger, target: target!, selector: selector!, userInfo: nil, repeats: false) } }
如果你需要一个重复的,只需要数学来计算定时器应该在后台触发的次数
let howManyTimes = abs(howMuchLonger) / repeatInterval
当我点击主页按钮并让我的应用程序运行背景
不,这是一个错误的假设。 点击主页按钮时, 不会让您的应用在后台运行。 您可以在后台暂停您的应用。 当您在后台时,您的代码不会运行 。
应用程序可以获得在后台运行的特殊权限,以便执行某些有限的活动,例如继续播放音乐或继续使用Core Location。 但你的应用程序没有做任何事情,所以它进入后台并停止。