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")
}
}
It's ok for the Simulator ,I will get continuous "Something cool" through I tapped the home button. But it worked out when I debug the app with my iPhone. I did't get anything when I tapped the home button and make my app run background. Someone told me I can play a long blank music to make my App run in the background.But I don't know how to play music in the background with swift @matt
You can use
beginBackgroundTaskWithExpirationHandler
to get some background execution time.Swift 3.0
This code was inspired by this answer, but I ported to swift.
This apparently only runs for 3 minutes on iOS 7+.
add this code in appdelegate for run a task in background , its working finely,
I'm willing to bet that you don't need to run a timer in the background, what you need is to know the time that has elapsed while you were suspended. That is a much better use of your resources than trying to play an empty sound file.
Use
applicationWillResignActive
andapplicationDidBecomeActive
to determine how much time has elapsed. First save the fireDate of your timer and invalidate it inapplicationWillResignActive
Next, determine how much time is left for the timer in
applicationDidBecomeActive
by comparing the time now to the fireDate you saved in theapplicationWillResignActive
call:If you need a repeating one, just do math to figure out how many times the timer should have fired while in the background
No, that's a false assumption. When you tap the home button, you do not make your app run in the background. You make your app suspend in the background. Your code does not run when you are in the background.
Apps can get special permission to run in the background just in order to perform certain limited activities, like continuing to play music or continuing to use Core Location. But your app does none of those things, so it goes into the background and stops.