I'd like to set a badge on the icon of my app like in apple's mail app (number on top of the icon). How can I do this in Swift (iOS8)?
5 Answers
The "number on top of the icon" is called a Badge. Badges can be set on a number of things besides Application Icons including Navigation Bar toolbar icons.
There are many ways to change the Application Icon Badge. Most use cases involve setting this when the Application is in the background to alert the user that there is some change that they may be interested in. This would involve a push notification.
For more on that see: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1
However, you can also change it while your app is active. You will need permission from the user by registering the UserNotificationType. Once you get permission, you can change it to whatever number you wish.
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
UIUserNotificationType.Badge, categories: nil
))
application.applicationIconBadgeNumber = 5
-
That's objective-c isn't it? – hans Jan 18 '15 at 15:10
-
1Yes, I translated it for you. Apologies for before. – ericgu Jan 18 '15 at 16:13
-
2You should add an explanation of what this code does. Otherwise, you're not teaching anyone anything. – SevenBits Jan 18 '15 at 16:40
-
1Thanks for your answer. But what's `application`? I get an error (`use of unresolved identifier 'application'`). – hans Jan 18 '15 at 19:07
-
2I put it in the didFinishLaunchingWithOptions in the AppDelegate File. You can also do this : let application = UIApplication.sharedApplication(). May have to import UIKit. – ericgu Jan 18 '15 at 19:17
-
I received push notification in all state i.e. when app is in forground, background, close(terminated)..! but my problem is that how to increment badge count when app is in background state and close (terminate) state???? Please tell me in details how i do it....? my app badge count is only increase when app is in forground state.....? if possible please tell me in brief.......!for Swift 3 – Kiran Jadhav Jul 29 '17 at 05:14
-
Feel free to edit. I am not working in Swift iOS nowadays – ericgu Jul 22 '19 at 13:07
2019
The answer is simply
UIApplication.shared.applicationIconBadgeNumber = 777
Unfortunately though it will not work unless you ask for permission first.
To do that you simply:
UNUserNotificationCenter.current().requestAuthorization(options: .badge)
{ (granted, error) in
if error == nil {
// success!
}
}
ericgu's answer seems to be outdated. looks like this -> | got replaced.
Here is a working Swift 2 code:
let badgeCount: Int = 0
let application = UIApplication.sharedApplication()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil))
application.applicationIconBadgeNumber = badgeCount
Edit: Swift 3:
import UIKit
import UserNotifications
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let badgeCount: Int = 10
let application = UIApplication.shared
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
application.registerForRemoteNotifications()
application.applicationIconBadgeNumber = badgeCount
}
}

- 16,783
- 19
- 105
- 136
-
Am i need to call this in a viewcontroller's view didload...? or can i call it in app delegate...? – Abirami Bala May 18 '17 at 05:27
-
-
I received push notification in all state i.e. when app is in forground, background, close(terminated)..! but my problem is that how to increment badge count when app is in background state and close (terminate) state???? Please tell me in details how i do it....? my app badge count is only increase when app is in forground state.....? if possible please tell me in brief.......!for Swift 3 – Kiran Jadhav Jul 29 '17 at 05:15
For iOS10, Swift 3 with backward-compatibility, you can wrap the best answers into a nice (static) utility function:
class func setBadgeIndicator(badgeCount: Int) {
let application = UIApplication.shared
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.badge, .alert, .sound]) { _, _ in }
} else {
application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
}
application.registerForRemoteNotifications()
application.applicationIconBadgeNumber = badgeCount
}

- 4,903
- 3
- 23
- 34

- 2,433
- 24
- 27
-
1
-
@PatelJigar, i don't think so. This badge function is designed to be updated via notifications – dy_ Dec 22 '16 at 16:18
-
5
-
1@Patal did you happen to try it on the same install that you had previously accepted permission for notifications. Try it on a fresh install... – quemeful May 16 '17 at 18:14
-
1@quemeful, may I know where this function should be called in App delegate...? – Abirami Bala May 18 '17 at 05:30
-
I received push notification in all state i.e. when app is in forground, background, close(terminated)..! but my problem is that how to increment badge count when app is in background state and close (terminate) state???? Please tell me in details how i do it....? my app badge count is only increase when app is in forground state.....? if possible please tell me in brief.......!for Swift 3 – Kiran Jadhav Jul 29 '17 at 05:15
-
Yes, you can update the badge without registering for remote notifications, just do: `application.applicationIconBadgeNumber =
` – kakubei Mar 20 '18 at 12:43