I have an application that starts the timer, and the timer should continue to work even if the application crashes or the phone is turned off. Therefore, I am trying to do this with shouldSaveApplicationStateand shouldRestoreApplicationState. I added both methods willFinishLaunchingWithOptionsto my appDelegate, and I set recovery identifiers for each view controller, navigation controller, and tab bar controller. Then on the view controller, I want to restore 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()
}
}
. Xcode, Xcode , , . , Xcode, , , . , , Xcode, . . Xcode, . , .
?