IOS7:
[objc] view plaincopy
- UIApplication *app = [UIApplication sharedApplication];
- // 应用程序右上角数字
- app.applicationIconBadgeNumber = 99;
上面的代码可以搞定
IOS8:
iOS8中设置application badge value 会抛错:Attempting to badge the application icon but haven‘t received permission from the user to badge the
原因是因为在ios8中,设置应用的application badge value需要得到用户的许可。使用如下方法咨询用户是否许可应用设置application badge value
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
在这之前要判断系统是否为iOS8的系统,否则8之前的系统会报错。
[objc] view plaincopy
- UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
- [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
- UIApplication *app = [UIApplication sharedApplication];
- // 应用程序右上角数字
- app.applicationIconBadgeNumber = 0;
注意:清除数字请将数字设置为 0
时间: 2024-10-06 19:27:51