在ios 9 中设置应用徽章编号

Set the application badge number in ios 9

我用过

[UIApplication sharedApplication].applicationIconBadgeNumber = 5;

用于设置应用徽章编号。仅在 ios 9 中不起作用。

谁能告诉我为什么?

我试过了,

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:5];

在你Appdelegate.m文件didFinishLaunching方法中设置...

application.applicationIconBadgeNumber = 5.

并将当前日期更改为 2 天后,然后重新启动 iPhone.. 并再次测试。它应该工作正常。

您好,您需要像这样注册 UIUserNotificationSettings

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

[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

[UIApplication sharedApplication].applicationIconBadgeNumber = 1;

Registering for Notification Types in iOS

In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload. Link: Docs

@property(nonatomic) NSInteger applicationIconBadgeNumber; // set to 0 to hide. default is 0. In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to set the icon badge.

所以我认为您必须为更改号码通知注册徽章。

它像这样完美地工作:

[UIApplication sharedApplication].applicationIconBadgeNumber = 10;

我试过 Xcode 7.2、iOS 9.2、iPhone 6s Plus。