Count current streak in swift - ios

I would like to count how many continuous days user has used the app.
It updates the label depending on the streaks and if user has not used the app for a day the number goes back to zero.
How can I achieve this? I have searched but could not find any source.

For this you have a few things to take into consideration:
When to report last usage?
Your app idea may include the need to perform some actions before
considering a complete usage. For example, after loading or presenting something on the screen, after retrieving data and performing some actions, etc.
Just by intention of opening the app. The only intention is for the user to hit your app´s icon to launch the app, nevermind if he set it to a close state before even passing your loading screen.
This can be a bit unpredictable
When sending the app to background.
Important to notice that iOS can kill your process anytime after your
app is sent to background, so better to do it right after user´s
action.
Also, the user could not open your app again in a while.
You can subscribe to background capabilities for letting your app be active for a while longer while transitioning to suspended/close state if you are going to save data out of the iPhone.
The function you are looking for is applicationDidEnterBackground(_:)
Strong Points of this approach
You get last time that your app was actually used.
For more on the application life cycle and how to handle it correctly, please visit apple documentation about this topic
Do I need this information to be available between installs & Where to save ?
If you care about this counter to be stable and remains intact between installs you can not save it in any local database or NSUserDefaults. In this case you should implement some kind of online storage, via user creation & handling in your servers or the use of iCloud alternatives.
If your information is sensitive (let's say that you are going to give some money like reward to your user for opening your app 1000 times) then you can not store it in NSUserDefaults, as it is not encripted and can be modified.
What to save in order to count days in a row?
Simplicity is king when dealing with stored data and there are many ways to achieve this specific task.
I would go with:
Storing the first date (ignoring time if you are dealing with calendar days, but including it if you are handling 24hours lapses as your day instead)
Storing last visit date (same considerations apply).
You could save complete timestamp in order to be able of change your mind later ;-)
In my app I would do the maths then with current date data (now = NSDate()) before making any changes.
If timelapse between now and last visit date is bigger than
a "Day", then update first visit date with now.
Save now data into last visit date storage.
Your counter will always be the difference in "Days" between now and first visit date.
Summing Up
If your data is not sensitive store it in NSUserDefaults, otherwise and if this can affect your income store it somewhere else. If it's sensitive but you don't care if your user lose the counter, save it in a local DB (CoreData, Realm, etc)
Best time (as of my consideration) for storing new data will be when an intention of closure (included suspended state and incoming calls) is notified to your app.
You can save this data in many ways, one that give you some space for maneuvering is saving just last visit and date of first visit of the row and then do the maths. Of course, updating as needed and explained before.

private extension Sequence where Element == Date {
var streaks: Int {
let oneDayInSeconds: Double = 60*60*24
let days = self.compactMap { Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: $0) }
let uniq = Set(days).sorted(by: >)
var count = 0
guard var lastStreak = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date()) else { return count }
for date in uniq {
guard date > lastStreak.advanced(by: -oneDayInSeconds - 1) else { break }
count += 1
lastStreak = date
}
return count
}
}

Simply save the last date in your UserDefault or CoreData, then on the next date, use timeIntervalSinceDate to get the timeInterval, divide to 3600, if that value is more than 24, reset the number

I didn't checked but this idea will work, simply store current date (DD-MM-YYYY) into Array or list & save it using any Data Persistence Options. Before adding date (Whenever launching app - you can use didFinishLauchingOptions) into list check whether it had already or not.To showing continuous days use the array count of saved list.
To reset Zero just check last date is Available or not.

This is my implementation for keeping track of a login streak and if that streak was rewarded yet:
class func checkForStreak() -> Int {
let lastLogin = UserDefaults.standard.string(forKey: "lastLogin")
guard let lastLogin = lastLogin else {
UserDefaults.standard.set(1, forKey: "loginStreak")
UserDefaults.standard.set(Date().toString(), forKey: "lastLogin")
return 1
}
let format = DateFormatter()
format.dateFormat = "YYYY-MM-DD"
guard let lastLoginDate = format.date(from: lastLogin) else {
return 0
}
guard let modifiedDate = Calendar.current.date(byAdding: .day, value: 1, to: lastLoginDate) else {return 0}
if lastLoginDate.isToday {
//login on same day
return UserDefaults.standard.integer(forKey: "loginStreak")
} else if modifiedDate.isToday {
//streak is extended
var streak = UserDefaults.standard.integer(forKey: "loginStreak")
streak += 1
UserDefaults.standard.set(streak, forKey: "loginStreak")
UserDefaults.standard.set(false, forKey: "streakRewarded")
return streak
} else {
//streak is broken
UserDefaults.standard.set(1, forKey: "loginStreak")
return 1
}
}
you'll want to set:
UserDefaults.standard.set(true, forKey: "streakRewarded")
after rewarding the streak. I call this function in my viewDidLoad of the root view controller.

func streakCount() {
if USERDEFAULTS.value(forKey: "lastLogin") == nil{
USERDEFAULTS.set(1, forKey: "loginStreak")
USERDEFAULTS.set(Date().toString(), forKey: "lastLogin")
return
}
let lastLogin = USERDEFAULTS.value(forKey: "lastLogin") as! String
let format = DateFormatter()
format.dateFormat = "yyyy-MM-dd"
guard let lastLoginDate = format.date(from: lastLogin) else {
return
}
guard let modifiedDate = Calendar.current.date(byAdding: .day, value: 1, to: lastLoginDate) else {
return
}
if modifiedDate.isToday {
//streak is extended
var streak = USERDEFAULTS.integer(forKey: "loginStreak")
streak += 1
USERDEFAULTS.set(streak, forKey: "loginStreak")
USERDEFAULTS.set(Date().toString(), forKey: "lastLogin")
} else {
//streak is broken
let streak = USERDEFAULTS.integer(forKey: "loginStreak")
if USERDEFAULTS.value(forKey: "longestStreak") != nil{
let longestStreak = USERDEFAULTS.integer(forKey: "longestStreak")
let streak = USERDEFAULTS.integer(forKey: "loginStreak")
if streak > longestStreak{
USERDEFAULTS.set(streak, forKey: "longestStreak")
}
}
else{
USERDEFAULTS.set(streak, forKey: "longestStreak")
}
USERDEFAULTS.set(0, forKey: "loginStreak")
}
}

Related

iOS: execute code AFTER a particular date

I have some code I want to run after a particular date/time has passed. For example, if I want the code to run 7 days from now and the user opens the app at any time on day 7 or after the code will run but if they open the app before the beginning of day 7 nothing happens. Timers in the main runloop work but only if the app is still running in the background. I need a method that will work even if the user kills the app.
Your best option is to store it as local data Even though you only want the code to run once, the overhead is so low, the "check" will not impact the speed or feel of the application. Also this will allow you to run additional checks .. If someone deletes the app, for instance, and leaves the local storage behind. If they re-install you could theoretically "remember" that the application has been installed, and said code has already run (until the user clears application data)
Something like:
//Globally set key
struct defaultsKeys {
static let keyDate = "dateKey"
}
// Set the date in local storage
let defaults = UserDefaults.standard
defaults.set("Your Date String", forKey: defaultsKeys.dateKey)
// Get the date from local storage
let defaults = UserDefaults.standard
if let stringDate = defaults.string(forKey: defaultsKeys.dateKey) {
print(stringDate)
// Do your date comparison here
}
Very few lines of code, and even though the check happens every time the application starts .. The overhead is negligible.
You can either set the date you want your app to "remember" on your local storage or web service. Then, when the user opens your app, compare that date to current device time to determine if you should execute your code.
First, save the current time when you want. You can set the key name however you want.
UserDefaults.standard.setValue(Date(), forKey: "rememberTime")
And every time I open the app, You compare the current time with the saved time.
To do so, I created a function that compares time.
extension Date {
func timeAgoSince() -> Bool {
let calendar = Calendar.current
let unitFlags: NSCalendar.Unit = [.day]
let components = (calendar as NSCalendar).components(unitFlags, from: self, to: Date(), options: [])
if let day = components.day, day >= 7 {
// Returns true if more than 7 days have passed.
return true
}
return false
}
}
Recall the previously saved time and use the time comparison function.
let beforeTime: Date = (UserDefaults.standard.object(forKey: "rememberTime") as? Date)!
if beforeTime.timeAgoSince() {
// more than seven days later
...
} else {
...
}
If you have a problem, please leave a comment !
You can use the below sample code:
override func viewDidLoad() {
super.viewDidLoad()
let nextCodeRunDate = Date() + (7 * 24 * 60 * 60) // 7 Days
if let savedDate = UserDefaults.standard.value(forKey: "NEXT_DATE") as? Date {
if Date() > savedDate {
UserDefaults.standard.setValue(nextCodeRunDate, forKey: "NEXT_DATE")
runYourCode()
}
}else {
// First time
UserDefaults.standard.setValue(nextCodeRunDate, forKey: "NEXT_DATE")
runYourCode()
}
}
func runYourCode() {
// Your code
}

How to add to a variable every 24 hours in iOS using Swift?

I'm completely lost in finding a tutorial or some sort of answer.
I'm trying to add a number (+1 for example), to a variable (that gets saved locally), every said amount of time (24 hours). Even if the user doesn't open/run the app.
Example: A mobile game named, "Cookie Clicker", kind of has this same functionality. It gives the user cookies depending on how much time was spent not playing the game/being offline.
My Question: How can I add +1 to a variable every 24 hours, regardless of if the user opens the app.
Code I Currently Have:
let daysOffDesfult = UserDefaults.standard
var daysOff = 0
//After 24 Hours: Call updateDaysOff() Function (This is the code I need.)
//Code Used to Save Variable Locally:
//Display the Updated Variable
if (daysOffDesfult.value(forKey: "daysOff") != nil){
daysOff = daysOffDesfult.value(forKey: "daysOff") as! NSInteger!
countLabel.text = "\(daysOff)"
}
//Update the Variable
func updateDaysOff() {
daysOff = daysOff + 1
countLabel.text = "\(daysOff)"
let daysOffDesfult = UserDefaults.standard
daysOffDesfult.setValue(daysOff, forKey: "daysOff")
daysOffDesfult.synchronize()
}
You can save the time of first open app in viewDidLoad or in your first UIViewController in UserDefaults like this:
if UserDefaults.standard.value(forKey: "firstDate") == nil {
UserDefaults.standard.set(Date(), forKey: "firstDate")
}
and every time app launches you compare the current date to date that you saved in UserDefaults to find out how many days passed
let savedDate = UserDefaults.standard.value(forKey: "firstDate")
let currentDate = Date()
let diffInDays = Calendar.current.dateComponents([.day], from: savedDate, to: currentDate).day

Saving data (numbers) in an iOS app?

I'm learning application development working on a quiz game. I'd like to add statistics to the game. For example, the average score since the app has been downloaded. How can I store the scores on the device in order to reuse them after the app has been closed?
You should take a look at UserDefault. It's basically a dictionary that persists until the user uninstalls your app. I like to write a wrapper around it to get strong typing and ease of reference:
struct Preferences {
static func registerDefaults() {
UserDefaults.standard.register(defaults: [kAverageScore: 0])
}
// Define your key as a constant so you don't have to repeat a string literal everywhere
private static let kAverageScore = "averageScore"
static var averageScore: Double {
get { return UserDefaults.standard.double(forKey: kAverageScore) }
set { UserDefaults.standard.set(newValue, forKey: kAverageScore) }
}
}
Here's how to use it: before you call it for the first time in your app, you must register the defaults. These are the values that your app ships with. On iOS, it only really matters for the very first time the user launches your app. On OS X, do this every time your app starts because the user can delete the app's preferences from ~/Library/Application Support.
// You usually do this in viewDidLoad
Preferences.registerDefaults()
From then on, getting and setting the property is easy:
let averageScore = Preferences.averageScore
Preferences.averageScore = 5.5
You should take a look at UserDefaults
Example
let defaults = UserDefaults.standard
defaults.set(25, forKey: "Age")
defaults.set(true, forKey: "UseTouchID")
defaults.set(Double.pi, forKey: "Pi")
To read values back
let age = defaults.integer(forKey: "Age")
let useTouchID = defaults.bool(forKey: "UseTouchID")
let pi = defaults.double(forKey: "Pi")
UserDefaults

How do I use a firebase server timestamp, with queryStartingAtValue, to track a user's current actions, for a firebase listener in iOS & Swift

I am developing an iOS app in Swift which has a like feature which is the same concept as liking a Facebook post, a Twitter Tweet etc.
I want to create a listener that only listens to the user liking posts with a timestamp that starts from now. i.e once they open the app. I wanted to use a firebase server timestamp value to get the current timestamp
I have the following structure in my database
"userLikes": {
"$uid":{
"$messageId": {
"timestamp": 1212121212121 // timestamp created using fb server
}
}
}
This was my attempted solution but it doesn't work and the problem I have is to do with -> FIRServerValue.timestamp()
let queryRef = ref.child("userLikes").child(uid).queryOrdered(byChild: "timestamp").queryStarting(atValue: FIRServerValue.timestamp())
queryRef.observe(.childAdded, with: { (snapshot) in
let utterId = snapshot.key
self.newsFeedModel.uttersInfo[utterId]?[Constants.UttersInfoKeys.isLikedByCurrentUser] = true as AnyObject
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}, withCancel: nil)
Any ideas how I Can implement this? Perhaps I should just use NSDate to get the current time for comparison but thought that using a firebase server timestamp would be the optimum way for comparison purposes.
You can estimate the ServerTime by taking the local time and correcting for the clock-skew and latency. The Firebase documentation has a section on Clock Skew.
Clock Skew
While firebase.database.ServerValue.TIMESTAMP is much more accurate, and preferable for most read/write ops, it can occasionally be useful to estimate the clients clock skew with respect to the Firebase Realtime Database's servers. We can attach a callback to the location /.info/serverTimeOffset to obtain the value, in milliseconds, that Firebase Realtime Database clients will add to the local reported time (epoch time in milliseconds) to estimate the server time. Note that this offset's accuracy can be affected by networking latency, and so is useful primarily for discovering large (> 1 second) discrepancies in clock time.
let offsetRef = FIRDatabase.database().referenceWithPath(".info/serverTimeOffset")
offsetRef.observeEventType(.Value, withBlock: { snapshot in
if let offset = snapshot.value as? Double {
let estimatedServerTimeMs = NSDate().timeIntervalSince1970 * 1000.0 + offset
}
})
This will likely work better than purely using a client-side timestamp, since that is likely to be different between clients.
Update for Swift 3/4:
public func getCurrentTime(completionHandler:#escaping (Double) -> ()){
Database.database().reference(withPath: ".info/serverTimeOffset").observeSingleEvent(of: .value) { (snapshot) in
if let time = snapshot.value as? Double{
completionHandler(Date().timeIntervalSince1970 + time)
}else{
completionHandler(0)
}
}
}
Usage:
getCurrentTime(){ (date) in
print(date)
}

Using NSUserDefaults to add a 24hour countdown timer in SWIFT

I am trying to add a timer for when a user clicks a button it starts a timer for 24 hours and disables the button for the next 24 hours. After that it is enabled again. There is a few answers out there for things similar but not 100% useful for doing it in SWIFT.
The main problem I am having is that I want this to be specific for each user. So 24 hours for every click on that one user. So for example: If I 'like' something then you want be able to 'like' that particular thing again for 24 hours but can still 'like' a different thing?
Thanks
*
Works for Swift 3
*
I have a daily video Ad that my users can view to get extra cash. This is what I use to ensure they can only view it once a day.
1.) Create a function that will be called when the user triggers it.
func set24HrTimer() {
let currentDate = NSDate()
let newDate = NSDate(timeInterval: 86400, since: currentDate as Date)
UserDefaults.standard.setValue(newDate, forKey: "waitingDate")
print("24 hours started")
//disable the button
}
2.) Create a variable at the top of your file.
let todaysDate = NSDate()
3.) In the viewDidLoad or didMoveToView call:
if let waitingDate:NSDate = UserDefaults.standard.value(forKey: "waitingDate") as? NSDate {
if (todaysDate.compare(waitingDate as Date) == ComparisonResult.orderedDescending) {
print("show button")
}
else {
print("hide button")
}
}
You can do it by setting the actual date + 1 day and save it into your NSUserDefaults:.
So in your button-pressed method, you can do something like that:
//user pressed button:
func buttonPressed(){
//current date
let currentDate = NSDate()
let calendar = NSCalendar.currentCalendar()
//add 1 day to the date:
let newDate = calendar.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: 1, toDate: currentDate, options: NSCalendarOptions.allZeros)
NSUserDefaults.standardUserDefaults().setValue(newDate, forKey: "waitingDate")
//disable the button
}
And to check the time you can retrieve the information. I would recommend to check it inside the AppDelegatemethods like applicationDidFinishLaunchingWithOptions.
//call it whereever you want to check if the time is over
if let waitingDate:NSDate = NSUserDefaults.standardUserDefaults().valueForKey("waitingDate") as? NSDate{
let currentDate = NSDate()
//If currentDate is after the set date
if(currentDate.compare(waitingDate) == NSComparisonResult.OrderedDescending){
//reenable button
}
}
A few things to consider first.
How important is it that this button can not be subverted?
If you are depending on the device for its current time and date, then the user can always just move that forward one day in the device settings.
Do you want any behavior to happen outside of your application?
should the user be notified that the button is now enabled
Assuming you don't need strictly enforce the 24 hour period, and you don't want to notify the user (they can find out when they return to your app), then you have only to do a few things.
Get a timeStamp when the button is pressed, start an NSTimer for 24Hours, and save the timeStamp to NSUserDefaults.
//Assuming you have a method named enableButton on self
let timer = NSTimer.scheduledTimerWithTimeInterval(86400, target: self, selector: "enableButton", userInfo: nil, repeats: false)
NSUserDefaults.standardUserDefaults().setObject(NSDate(), forKey: "timeStamp")
Now if the user never leaves your app, your good. In real life they will, so you will need to check when you re-enter your app if you need to disable the button, based on the timeStamp and start a new timer for the amount of time that is left.

Resources