Change your App Icon with predefined periods— iOS Swift

Change the image regardless of the application version.

Bruno Chen Chih Ying
2 min readDec 28, 2020

If your projects use Firebase Realtime, use this guide.

This time we will use predefined periods for projects that do not use Firebase Realtime. Here are the 5 steps to add this cool feature to your App.

Let’s get started.

Step 1: Add your App Icons

To get started, we need some icons. These should be placed loose in your project or in a group, rather than inside an asset catalogue. Remember to use the @2x and @3x naming convention to ensure iOS automatically picks the correct icon for users’ devices.

For this example, we’re using these example icon files:

  • Icon-blue@2x.png, Icon-blue@3x.png
  • Icon-red@2x.png, Icon-red@3x.png

They are all just regular PNGs, with the @2x being 120x120 and the @3x being 180x180.

Step 2: Register your new Icons in the Info.plist

First in Info.plist, add a new CFBundleIcons (this will change to "Icon files (iOS 5)"), then add another entry CFBundleAlternateIcons.

Inside CFBundleAlternateIcons add all alternate icon you have. For each alternate icon, add a new entry CFBundleIconFiles array, which is an array containing the filenames for your icon.

We can define the primary icon as in the image.

Here is the xml code added from info.plist:

Step 3: Create the class to record/check events

1- Create enum with all the icons:

2- Create class func and define the way we want to add events:

3- Add function that checks if there is an event today (using parseData func to convert String to Date), if there is an event it will call the function that changes the icon setAppIcon :

4- setAppIcon change the icon using UIApplication.shared.setAlternateIconName(name):

The full code is here:

The hasEventNow variable was added to make it more performative.

Obs.: For some cases we will need to set the delay to work 100%

Step 5: Using

It’s so easy, you just need to call class func setIconIfNeeded where you want.

See this project in the git:

That’s all, folks!! 😉

--

--