I have an app that runs a timer, and the timer should keep running even if the app quits or the phone is turned off.
So I'm trying to do that using shouldSaveApplicationState
and shouldRestoreApplicationState
. I added both methods and willFinishLaunchingWithOptions
to my appDelegate and I set up restoration IDs for every view controller, navigation controller and tab bar controller involved.
Then on the view controller I want to restore I did this:
override func encodeRestorableStateWithCoder(coder: NSCoder) {
coder.encodeObject(startDate, forKey: "startDate")
coder.encodeObject(startTime, forKey: "startTime")
coder.encodeObject(elapsedTime, forKey: "elapsedTime")
coder.encodeObject(playing, forKey: "playing")
coder.encodeObject(timerLabel.text, forKey: "timerLabelText")
super.encodeRestorableStateWithCoder(coder)
}
override func decodeRestorableStateWithCoder(coder: NSCoder) {
startDate = coder.decodeObjectForKey("startDate") as! NSDate
startTime = coder.decodeObjectForKey("startTime") as! NSTimeInterval
elapsedTime = coder.decodeObjectForKey("elapsedTime") as! NSTimeInterval
playing = coder.decodeObjectForKey("playing") as! Bool
timerLabel.text = (coder.decodeObjectForKey("timerLabelText") as! String)
super.decodeRestorableStateWithCoder(coder)
}
override func applicationFinishedRestoringState() {
if playing {
elapsedTime += startDate.timeIntervalSinceNow
play()
}
}
Now here's the weird part. When my phone is connected to Xcode and I use Xcode's play and stop buttons to start and to quit the app everything works as it should. When I try the same thing with the phone disconnected from Xcode, though, it's like I didn't even set up the state restoration at all, the app ignores it completely and just shows the first view controller. And I can't even debug because when I connect the phone to Xcode it works out. The same thing happens on the simulator. If I use Xcode's buttons the restoration works. If I just open and close the app from the simulator itself, it doesn't.
Any ideas?