陈斌彬的技术博客

Stay foolish,stay hungry

iOS8 设置 Application Badge Value

在 iOS8 中,直接设置application badge value会出错

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];

曝出” Attempting to badge the application icon but haven’t received permission from the user to badge the application “的错误。

原因是因为在 iOS8 中,设置应用的 application badge value 需要得到用户的许可。使用如下方法咨询用户是否许可应用设置application badge value

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

程序运行时,会弹出提示框给用户

img

之后再通过

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];

设置 application badge value