Update every day in quick syntax
Working on an app for quotes and as a newbie I decided to eliminate the use of CoreData and Sqlite in my app. So I decided to try the collection and change the text label. I have a collection stored in an array. I am trying to achieve a text change every 24 hours and it changes at 8:00 AM EST (so 8 AM to 8 AM). I would like the outline to be something like
quoteindex = 0
if(time_elasped:24 hours something about 8:00 A.M EST) {
quote.text = quoteCollection.quoteArray[quoteIndex]
quoteindex++ (next quote in array)
}
How can I organize something like this in terms of syntax? Would I use a different loop?
source to share
An easy way to do this would be to use NSUserDefaults to store an NSDictionary containing the last time and index of when the user last received a quote.
in viewDidLoad: (or make a standalone function - checkLastRetrieval ())
let userDefaults = NSUserDefaults.standardUserDefaults()
if let lastRetrieval = userDefaults.dictionaryForKey("lastRetrieval") {
if let lastDate = lastRetrieval["date"] as? NSDate {
if let index = lastRetrieval["index"] as? Int {
if abs(lastDate.timeIntervalSinceNow) > 86400 { // seconds in 24 hours
// Time to change the label
var nextIndex = index + 1
// Check to see if next incremented index is out of bounds
if self.myQuoteArray.count <= nextIndex {
// Move index back to zero? Behavior up to you...
nextIndex = 0
}
self.myLabel.text = self.myQuoteArray[nextIndex]
let lastRetrieval : [NSObject : AnyObject] = [
"date" : NSDate(),
"index" : nextIndex
]
userDefaults.setObject(lastRetrieval, forKey: "lastRetrieval")
userDefaults.synchronize()
}
// Do nothing, not enough time has elapsed to change labels
}
}
} else {
// No dictionary found, show first quote
self.myLabel.text = self.myQuoteArray.first!
// Make new dictionary and save to NSUserDefaults
let lastRetrieval : [NSObject : AnyObject] = [
"date" : NSDate(),
"index" : 0
]
userDefaults.setObject(lastRetrieval, forKey: "lastRetrieval")
userDefaults.synchronize()
}
You can be more specific using NSDate if you want to enforce specific times (like 8AM) or make sure there is a unique quote for each actual day (Monday, Tuesday, etc.). This example just changes the label if the user saw the quote more than 24 hours ago.
Check out the docs for NSUserDefaults .
EDIT : If you want to notify the user at 8am the next day of a new quote, you can send the user a local notification .
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: someTimeInterval)
notification.timeZone = NSCalender.currentCalendar().timeZone
notification.alertBody = "Some quote" // or "Check the app"
notiication.hasAction = true
notification.alertAction = "View"
application.scheduleLocalNotification(notification)
You will need to calculate timeInterval so that there is 8AM left for the next day. Check out this answer: fooobar.com/questions/2248113 / ... (it's objective-c, but you have to figure it out)
EDIT
To execute the code when your view comes to the fore, you need to send a notification to your AppDelegate method of applicationWillEnterForeground. And add an observer for this notification to your view controller.
in AppDelegate
let notification = NSNotification(name: "CheckLastQuoteRetrieval", object: nil)
NSNotificationCenter.defaultCenter().postNotification(notification)
in ViewController
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("checkLastRetrieval"), name: "CheckLastQuoteRetrieval", object: nil)
checkLastRetrieval()
}
source to share