How to correctly set application badge value in iOS 8?

前端 未结 5 1726
故里飘歌
故里飘歌 2020-12-24 06:38

Old method

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];

is now gives error Attempting to badge the applic

相关标签:
5条回答
  • 2020-12-24 07:26

    I write a class to handle it when I use 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
        }
    }
    
    0 讨论(0)
  • 2020-12-24 07:27

    update to 8.3, ObjC: we should add Daij-Djan script to replace NSLog(@"access denied for UIUserNotificationTypeBadge"); in Spidy & KepPM solution above. Hope this help s.o.

    0 讨论(0)
  • 2020-12-24 07:30

    Additional info for previous posts (in complete to registerUserNotificationSettings):

    Apple makes new API for registering notifications and working with badges.

    See WWDC 2014 session video , text version and documentation.

    User can change permissions for every UIUserNotificationType (UIUserNotificationTypeBadge, UIUserNotificationTypeSound, UIUserNotificationTypeAlert) in Settings.

    Before changing badge you must check permissions.

    Code sample from my 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)
    

    The CurrentUserNotificationSettings method is available in the UI application instance and will give you the most up-to-date user notification preferences.

    Working with badge number:

    [self setApplicationBadgeNumber:0];
    

    instead of

    application.applicationIconBadgeNumber = 0;
    

    PS: Checking at compiling (#ifdef __IPHONE_8_0) due to the need to build in Xcode5 and Xcode6. If you do not have this need, the code can be simplified.

    0 讨论(0)
  • 2020-12-24 07:32

    In addition to Daij-Djan's answer: it's possible to stack the enums so you can request them all at once. Like follows:

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

    Debug output mentions I should ask for Application Badge permission

    0 讨论(0)
  • 2020-12-24 07:36

    to modify the badge under ios8 you have to ask for permissions

        let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    

    or in objC

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
    0 讨论(0)
提交回复
热议问题