Timer application in ios using swift

Last updated on Oct 19, 2016

Milan Sanathara




  1. Create Timer
    1. Circular Progress
    2. Timer
    3. Timer Button
    4. Timer Text
  2. Time Out
    1. Alert User
    2. Reset Timer
  3. Store User Timer Setting
    1. Add tabs
    2. NSUserDefaults
  4. Apply Color
    1. Set Circular Progress Color
    2. Button Color

Create Timer

A Timer is a core part in this project. For that, add the KDCircularProgress in the project. Once you are done, then go to your storyboard view controller and add the view object from the object library and assign KDCircularProgress custom class from the identity inspector tab.

Now connect this progress view to view controller class, For example @IBOutlet weak var circularProgressView: KDCircularProgress!.

For timer you need total time and current time, these two variables are used for calculating remaining time.

We set timeInSec = totalTimeInSec! because when our app is launched timer is set to default time

For start and stop action, we need button. For that add new button in your storyboard and connect to view controller class.

Start Timer by default button name is “start”. In timer action function we check that if button text is start then called resetCounter function and activate timer and set button text to stop.

Stop Timer in timerAction else condition called when user tap button for stoping timer. To stop timer called its invalidate method called and then set button text to “start”. When timer stop reset our circularProgress for that set its angle to 0

Reset Timer called the resetCounter method when we want to reset our timer

For Display Remaining time we need label, add new lable in your storyboard and connect to view controller class

When our timer is start we need to change our label text every second to do this we make function called changeLabelText. This function is called by timer every second timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(TimerController.changeLabelText), userInfo: nil, repeats: true).

In changeLabelText function first we decrease our timer and set progress, then set to new time to label

Time Out

When time out, Show alter to user that time is over.

To check timer is over or not we use the swift property observers feature. Set observer in timerInSec property. If timerInSec value is less than or equal to zero called alter function

When time out, Show alter to user and reset timer.

When timerInSec is less than zero called timer!.invalidate() to stop timer and set button text to “start”. Here we no need to called resetCounter() because when user start again timer its called.

Store User Timer Setting

We work only with our timer controller with timer 60s, by adding new tab we give option to user so he/she can chnage the timer e.g. 5min timer.

Now open your main storyboard file and select the timer controller. Now goto editor menu and selct tab bar controller from embed in menu

Once tab bar controller is successfully added, add new view controller to your storyboard called set timer. Add new text field object from object library for timer and connect this text field to its controller class e.g. @IBOutlet weak var timeValue: UITextField!.

NSUserDefaults class, you can save settings and data related to application. For example, you could save a user email, username or a default color scheme for the application. The objects will be saved in what is known as the iOS “defaults system”. The iOS defaults system is available throughout all of the code in your app, and any data saved to the defaults system will persist through application sessions. This means that even if the user closes your application or reboots their phone, the saved data will still be available the next time they open the app!

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. Applications record such preferences,username by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default.

At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user’s defaults database.

In your project create new swift file called userDefault and add the following code

saveToUserDefault function is used to store data in user default. In NSUserDefaults data stored as key-pair value combination, so our function take two parameters, one is key and second is value. Both parameters are string type.

getFromUserDefault function is used to get data from user default. This function take one parameters that is key. If key found then return its data else return the nill.

Now use these function in our set timer controller, when our view is load get the user timer setting and set to text field.

When user leave the view or switch to timer tab, save text field data to user default

Next step is to modify our timer controller, so that timer is set according to user setting. For that in use init function.

When our class is initializing its called init function, in this function we check that if we date from user default then multiply by 60.0 because we need total seconds else set 1 (1 min) to use default.

We need to modify our resetCounter function also.

Apply Color

Our timer app is ready now its time to apply some color. Our CircularProgress has IBInspectable and IBDesignable support, so no need to go find property and replace from code file, you can configure and preview inside the Interface Builder.

Go to your storyboard file and select circularProgressView, open attributes inspector tab and find track color property and set color light gray.

To set progress color go to ibcolor1 property and to green color.

Select timer action button and go to attributes inspector tab. Set button text color to green.


I hope you will find this post very useful regarding timer application in swift. Let me know if you have any question regarding timer application in comment . I will reply to you ASAP.

Have you got cool idea about iPhone App Development? Contact us Now to get free consultation on your idea. Alphanso Tech is rated as one of the best iPhone App Development Company in India.

Post by Milan Sanathara

I am iOS Application Developer with an aspiration of learning new technology and creating a bright future in Information Technology.

Leave a Reply

Your email address will not be published. Required fields are marked *