Quick use of NSTimer background?

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") } } 

This is normal for the Simulator, I will get a continuous “Something Cool” when I tapped the home button. But it worked when I debug the application using my iPhone. I got nothing when I clicked the home button and made a background image of the application. Someone told me that I can play long empty music so that my application starts in the background. But I do not know how to play music in the background at @matt speed

+6
source share
4 answers

You can use beginBackgroundTaskWithExpirationHandler to get some background execution time.

 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!) }) 

This code was inspired by this answer , but I put it in a quick one.

This apparently only works for 3 minutes on iOS 7+.

+18
source

I bet you don't need to start the timer in the background, you need to know the time elapsed during the suspension. It is much better to use your resources than trying to play an empty sound file.

Use applicationWillResignActive and applicationDidBecomeActive to determine how much time has passed. First save the firedate of your timer and invalidate it in applicationWillResignActive

 func applicationWillResignActive(application: UIApplication) { guard let t = self.timer else { return } nextFireDate = t.fireDate t.invalidate() } 

Then determine how much time is left for the timer in applicationDidBecomeActive by comparing the time with FireDate that you saved in the applicationWillResignActive call:

 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) } } 

If you need a repetitive, just do the math to find out how many times the timer had to fire in the background

 let howManyTimes = abs(howMuchLonger) / repeatInterval 
+7
source

when I tapped the home button and made a background image of the application

No, this is a false assumption. When you press the home button, you do not force the application to run in the background. You force the application to pause in the background. Your code does not run when you are in the background.

Applications may receive special permission to run in the background only to perform certain limited actions, such as continuing to play music or continuing to use Core Location. But your application does nothing, so it goes into the background and stops.

+3
source

add this code to appdelegate to run the task in the background,

 var backgroundUpdateTask: UIBackgroundTaskIdentifier = 0 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { return true } func applicationWillResignActive(application: UIApplication) { self.backgroundUpdateTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ self.endBackgroundUpdateTask() }) } func endBackgroundUpdateTask() { UIApplication.sharedApplication().endBackgroundTask(self.backgroundUpdateTask) self.backgroundUpdateTask = UIBackgroundTaskInvalid } func applicationWillEnterForeground(application: UIApplication) { self.endBackgroundUpdateTask() } 
0
source

Source: https://habr.com/ru/post/980851/


All Articles