-2

How can I update the badge for a specific UITabBarItem when a new push notification arrives? For example like the activity tab in Instagram

Gil
  • 559
  • 6
  • 18

2 Answers2

1

You should use the following code:

[[[[[self tabBarController] tabBar] items] 
               objectAtIndex:tabIndex] setBadgeValue:badgeValueString];
Ritu
  • 661
  • 3
  • 8
  • Yes but how do I access the tabbarController from the AppDelegate where the push notification is handled? – Gil May 15 '15 at 08:54
  • Where you have created tabbarcontroller? I guess, it should be in AppDelegate – Ritu May 15 '15 at 08:55
  • I just dragged it to the storyboard – Gil May 15 '15 at 08:56
  • Please check : self.window.rootViewController. Is it tabbarcontroller instance? – Ritu May 15 '15 at 09:00
  • rootViewController is a UIView. When I try to access rootViewController.tabBarController I get nil – Gil May 15 '15 at 09:14
  • How rootviewcontroller can be UIView? It can be UIViewController or UINavigationController or UITabbarController. – Ritu May 15 '15 at 09:20
  • Sorry I meant UIViewController :) – Gil May 15 '15 at 09:27
  • In AppDelegate, Can you get the instance of ViewController, currently present? If yes, you have to get it and check currentViewController.tabbarController . If not, rootviewController must be a UINavigationController object and you can get [[navController.viewControllers lastObject] tabbarController] will provide the tabbar controller instance – Ritu May 15 '15 at 09:35
1
NSString *badgeValue = [NSString stringWithFormat:@"%lu", (long)countUnreadMessages];

[[[[self viewControllers] objectAtIndex:indexOfTabbarItem] tabBarItem] setBadgeValue:([badgeValue isEqualToString:@"0"]) ? nil : badgeValue];
Utsav Parikh
  • 1,216
  • 7
  • 14