KepPM..
22
以前帖子的附加信息(完整版registerUserNotificationSettings):
Apple为注册通知和使用徽章制作了新的API.
请参阅WWDC 2014会话视频,文本版本
和文档.
用户可以更改每个权限UIUserNotificationType(UIUserNotificationTypeBadge,UIUserNotificationTypeSound,UIUserNotificationTypeAlert中设置).
在更改徽章之前,您必须检查权限.
来自AppDelegate的代码示例:
#ifdef __IPHONE_8_0
- (BOOL)checkNotificationType:(UIUserNotificationType)type
{
UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
return (currentSettings.types & type);
}
#endif
- (void)setApplicationBadgeNumber:(NSInteger)badgeNumber
{
UIApplication *application = [UIApplication sharedApplication];
#ifdef __IPHONE_8_0
// compile with Xcode 6 or higher (iOS SDK >= 8.0)
if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
{
application.applicationIconBadgeNumber = badgeNumber;
}
else
{
if ([self checkNotificationType:UIUserNotificationTypeBadge])
{
NSLog(@"badge number changed to %d", badgeNumber);
application.applicationIconBadgeNumber = badgeNumber;
}
else
NSLog(@"access denied for UIUserNotificationTypeBadge");
}
#else
// compile with Xcode 5 (iOS SDK < 8.0)
application.applicationIconBadgeNumber = badgeNumber;
#endif
}
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
该CurrentUserNotificationSettings方法在UI应用程序实例中可用,并将为您提供最新的用户通知首选项.
使用徽章编号:
[self setApplicationBadgeNumber:0];
代替
application.applicationIconBadgeNumber = 0;
PS:#ifdef __IPHONE_8_0由于需要构建Xcode5和Xcode6,检查compiling().如果您没有这个需求,可以简化代码.