6
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

Ketan P
  • 4,259
  • 3
  • 30
  • 36
David Wang
  • 934
  • 1
  • 12
  • 16

4 Answers4

19

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 ported to swift.

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

fernandosavio
  • 9,849
  • 4
  • 24
  • 34
garetmckinley
  • 1,122
  • 2
  • 11
  • 29
  • As stated, it was a quick port from a previous answer. I just ported the answer, tested the code, and it appeared to be working. I can clean up the example a bit and remove some junk code. – garetmckinley Jan 12 '15 at 04:00
  • 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 @mediachicken – David Wang Jan 12 '15 at 06:18
  • 1
    @王怡飞 Maybe it's time you step back and ask your self if that's really the best approach. It seems like a pretty hacky method. What exactly are you trying to achieve? – garetmckinley Jan 12 '15 at 21:46
  • @mediachicken just a timer but I want it still running even I tap the home button. – David Wang Jan 13 '15 at 02:48
  • 1
    @王怡飞 Ugh. So don't let the user play music on their device while your app is running in the background? Lol. And Apple approved an app for "music" background mode, even though it doesn't play music?!? – Rob Jan 14 '15 at 15:06
  • Working Well for me!! – Antony Raphel Jun 08 '17 at 04:49
  • How to increase the time, you said "This apparently only runs for 3 minutes on iOS 7+." i need to increase this time any method ? – Midhun Narayan Mar 22 '19 at 10:30
7

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 and applicationDidBecomeActive to determine how much time has elapsed. 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()
}

Next, determine how much time is left for the timer in applicationDidBecomeActive by comparing the time now to the fireDate 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 repeating one, just do math to figure out how many times the timer should have fired while in the background

let howManyTimes = abs(howMuchLonger) / repeatInterval
Good Doug
  • 2,072
  • 15
  • 12
4

when I tapped the home button and make my app run 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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 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 – David Wang Jan 12 '15 at 06:15
  • Then learn how to do that. I did mention playing music as a legal background activity in my answer. – matt Jan 12 '15 at 07:18
  • I suspect your app may be rejected from the app store if you do something like that. Please instead explain what it is that you feel you need to do in the background? By default, a suspended app is not something that the user cares about at that moment. It should be able to do what it needs to when the user starts using it again. – Jeremy Huddleston Sequoia Jan 12 '15 at 08:45
  • suppose i don't need any location or music. i just want to know server that my app is running in background continuesly so how can i do that i just want to call webservice every 10 sec when app is in background mode – Jaydeep Vyas Dec 01 '18 at 08:37
0

add this code in appdelegate for run a task in background , its working finely,

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