app图标上的badge
1、授权
因为在IOS8中要想设置applicationIconBadgeNumber,需要用户的授权,在IOS8中,需要加上下面的代码:
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
但是这两句如果是在IOS7的系统中运行的话就会报错,所以需要先判断IOS的版本,完整代码如下:
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
2、设置值
[UIApplication sharedApplication].applicationIconBadgeNumber = 1
Tab Bar Item 的badge
#如果你的根视图控制器就是UITabBarController
//获取根视图控制器
UITabBarController *tabController = (UITabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
//获取 UITabBarController 指向的视图控制器
UIViewController *requiredViewController = [tabController.viewControllers objectAtIndex:index];
//获取tabBarItem
UITabBarItem *item = requiredViewController.tabBarItem;
//设置值
[item setBadgeValue:@"1"];
#如果你是UIViewController进入UITabBarController
UIViewController *rootController = self.window.rootViewController;
UITabBarController *tabController = rootController.tabBarController;
UIViewController *requiredViewController = [tabController.viewControllers objectAtIndex:1];
UITabBarItem *item = requiredViewController.tabBarItem;[item setBadgeValue:@"1"];