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

Rom*_*uba 36 ios8

老方法

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];
Run Code Online (Sandbox Code Playgroud)

现在给出了错误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];
Run Code Online (Sandbox Code Playgroud)

但是我收到了错误 <CKError 0x165048a0: "Not Authenticated" (9/1002); "This request requires an authenticated account">

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

Dep*_*o B 39

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

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
Run Code Online (Sandbox Code Playgroud)

调试输出提到我应该要求Application Badge权限


Dai*_*jan 36

要修改ios8下的徽章,您必须要求权限

    let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
Run Code Online (Sandbox Code Playgroud)

或者在objC

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
Run Code Online (Sandbox Code Playgroud)

  • @PsychoDad:您可以使用mcsheffrey建议的方法获取用户在显示警报时授予的权限.但是,用户以后可以进入设置并禁用某些权限.因此,获取当前权限的另一种方法是:`UIUserNotificationSettings*currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]` (5认同)

Kep*_*pPM 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)
Run Code Online (Sandbox Code Playgroud)

CurrentUserNotificationSettings方法在UI应用程序实例中可用,并将为您提供最新的用户通知首选项.

使用徽章编号:

[self setApplicationBadgeNumber:0];
Run Code Online (Sandbox Code Playgroud)

代替

application.applicationIconBadgeNumber = 0;
Run Code Online (Sandbox Code Playgroud)

PS:#ifdef __IPHONE_8_0由于需要构建Xcode5和Xcode6,检查compiling().如果您没有这个需求,可以简化代码.