Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count current streak in swift

Tags:

ios

swift

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. enter image description here

like image 972
risa8 Avatar asked Dec 24 '22 17:12

risa8


1 Answers

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.

  1. If timelapse between now and last visit date is bigger than a "Day", then update first visit date with now.
  2. 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.

like image 175
Hugo Alonso Avatar answered Jan 07 '23 12:01

Hugo Alonso