6

I'm writing a method to check if the current user settings consist of certain notification types.

When checking whether the current settings contain UIUserNotificationsType.None, it returns true for both when the permission was given and denied. Would anyone know why this is?

func registerForAllNotificationTypes()
{
    registerNotificationsForTypes([.Badge, .Alert, .Sound])
}

func registerNotificationsForTypes(types:UIUserNotificationType)
{
    let settings = UIUserNotificationSettings.init(forTypes:types, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}

func isRegisteredForAnyNotifications() -> Bool
{
    let currentSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
    print(currentSettings)
    print((currentSettings?.types.contains(.Alert))!)
    print((currentSettings?.types.contains(.Badge))!)
    print((currentSettings?.types.contains(.Sound))!)
    print((currentSettings?.types.contains(.None))!)

    return (currentSettings?.types.contains(.Alert))! //Just testing .Alert for now
}

When permission is on:

Optional(<UIUserNotificationSettings: 0x7fabdb719360; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);>) true true true true

When permission is off:

Optional(<UIUserNotificationSettings: 0x7f96d9f52140; types: (none);>) false false false true

1
  • I feel like it's behaving normally (given how it's implemented) but feels like a bug because of the misleading .None. If .None is ALWAYS true, then why do we have it to begin with? Commented Apr 19, 2016 at 18:24

1 Answer 1

2

Funny thing, it just confirms that 0 contains 0 :) Take a look on enum definition for UIUserNotificationsType: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIUserNotificationSettings_class/index.html#//apple_ref/c/tdef/UIUserNotificationType

struct UIUserNotificationType : OptionSetType {
    init(rawValue rawValue: UInt)
    static var None: UIUserNotificationType { get }
    static var Badge: UIUserNotificationType { get }
    static var Sound: UIUserNotificationType { get }
    static var Alert: UIUserNotificationType { get }
}

But it's more clearly visible in Objective-C:

typedef enum UIUserNotificationType : NSUInteger {
   UIUserNotificationTypeNone    = 0,
   UIUserNotificationTypeBadge   = 1 << 0,
   UIUserNotificationTypeSound   = 1 << 1,
   UIUserNotificationTypeAlert   = 1 << 2,
} UIUserNotificationType;
1
  • I get that. Yet in my head I expected that using the contains api alone will be enough to check whether the user has not given permission. I wonder if this is a known / accepted behavior
    – micap
    Commented Aug 27, 2015 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.