iOS中设置applicationIconBadgeNumber

  • 在iOS7中直接设置applicationIconBadgeNumber没有问题,但是在iOS8之后设置applicationIconBadgeNumber,程序没有反应.
  • 因为在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];
}
  • 如果是注册消息推送的话,需要这样写:
float version = [[[UIDevice currentDevice] systemVersion] floatValue];

if (version >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
04-18 11:51