如何在iOS 8中正确设置应用程序徽章值?

时间:2014-06-03 17:12:04

标签: ios8

旧方法

[[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">

如何设置徽章或获得一些新权限?

5 个答案:

答案 0 :(得分:39)

除了Daij-Djan的回答:它可以堆叠枚举,以便您可以立即请求它们。如下:

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

Debug output mentions I should ask for Application Badge permission

答案 1 :(得分:36)

修改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];

答案 2 :(得分:22)

以前帖子的附加信息(完整到registerUserNotificationSettings):

Apple为注册通知和使用徽章制作了新的API。

参见WWDC 2014会话videotext versiondocumentation

用户可以在“设置”中更改每个UIUserNotificationTypeUIUserNotificationTypeBadgeUIUserNotificationTypeSoundUIUserNotificationTypeAlert)的权限。

在更改徽章之前,您必须检查权限。

来自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)

UI应用程序实例中提供了CurrentUserNotificationSettings方法,它将为您提供最新的用户通知首选项。

使用徽章编号:

[self setApplicationBadgeNumber:0];

而不是

application.applicationIconBadgeNumber = 0;

PS:检查编译(#ifdef __IPHONE_8_0)是否需要构建Xcode5和Xcode6。 如果您没有这个需求,可以简化代码。

答案 3 :(得分:1)

当我使用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
    }
}

答案 4 :(得分:0)

更新到8.3,ObjC:我们应该添加Daij-Djan脚本来替换NSLog(@“对UIUserNotificationTypeBadge拒绝访问”);在Spidy&amp;以上KepPM解决方案。希望这有助于s.o。