如何在iOS 8中正确设置应用程序徽章值?
旧的方法
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];
现在给出错误Attempting to badge the application icon but haven't received permission from the user to badge the application
。
然后,我试图使用新的API(我认为是相关的徽章价值)
CKModifyBadgeOperation * operation = [[CKModifyBadgeOperation alloc] initWithBadgeValue:50]; [operation setModifyBadgeCompletionBlock:^(NSError *error) { NSLog(@"%@", error); }]; [operation start];
但是我收到错误<CKError 0x165048a0: "Not Authenticated" (9/1002); "This request requires an authenticated account">
<CKError 0x165048a0: "Not Authenticated" (9/1002); "This request requires an authenticated account">
如何设置徽章或获得一些新的权限?
除了Daij-Djan的回答之外,还可以将枚举堆叠起来,以便一次请求所有枚举。 如下所示:
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
debugging输出提到我应该要求应用程序徽章权限
修改ios8下的徽章,你必须要求权限
let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge, categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings)
或在objC
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
以前的post的附加信息(完成registerUserNotificationSettings
):
苹果公司为注册通知和使用徽章制作了新的API。
参见WWDC 2014会议video , 文本版本和文档 。
用户可以在设置中更改每个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中编译。 如果你没有这个需求,代码可以简化。
当我使用swift时,我写了一个类来处理它:
class ZYUtility { /// Set badge class func setApplicationBadgeNumber(badge: Int) { if ZYUtility.SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO("8.0") { if UIApplication.sharedApplication().currentUserNotificationSettings().types & UIUserNotificationType.Badge != nil { UIApplication.sharedApplication().applicationIconBadgeNumber = badge } else { println("No permission to set badge number") } } else { UIApplication.sharedApplication().applicationIconBadgeNumber = badge } } /// System check class func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool { return UIDevice.currentDevice().systemVersion.compare(version, options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedSame } class func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool { return UIDevice.currentDevice().systemVersion.compare(version, options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending } class func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool { return UIDevice.currentDevice().systemVersion.compare(version, options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending } class func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool { return UIDevice.currentDevice().systemVersion.compare(version, options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedAscending } class func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool { return UIDevice.currentDevice().systemVersion.compare(version, options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedDescending } }
更新到8.3,ObjC:我们应该添加Daij-Djan脚本来replaceNSLog(@“拒绝访问UIUserNotificationTypeBadge”); 在上面的Spidy&KepPM解决scheme中。 希望这样的帮助