2015-08-27 1 views
6

Sto scrivendo un metodo per verificare se le impostazioni utente correnti sono costituite da determinati tipi di notifica.Perché UIUserNotificationType.None restituisce true nelle impostazioni correnti quando viene concessa l'autorizzazione dell'utente?

Quando si controlla se le impostazioni correnti contengono UIUserNotificationsType.None, restituisce true per entrambi quando il permesso è stato dato e negato. Qualcuno saprebbe perché è così?

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 
} 

Quando l'autorizzazione è acceso:

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

quando il permesso è spento:

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

+0

mi sento come se fosse comportarsi normalmente (dato come è implementato), ma si sente come un bug a causa del fuorviante '.None'. Se '.None' è SEMPRE vero, allora perché lo abbiamo per cominciare? –

risposta

2

cosa divertente, ma conferma solo che contiene 0 0 :) Take a guarda la definizione di enum per 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 } 
} 

Ma è più chiaramente visibile in Objective-C:

typedef enum UIUserNotificationType : NSUInteger { 
    UIUserNotificationTypeNone = 0, 
    UIUserNotificationTypeBadge = 1 << 0, 
    UIUserNotificationTypeSound = 1 << 1, 
    UIUserNotificationTypeAlert = 1 << 2, 
} UIUserNotificationType; 
+0

L'ho capito. Eppure nella mia testa mi aspettavo che l'uso della sola contiene API fosse sufficiente per verificare se l'utente non ha dato il permesso. Mi chiedo se questo è un comportamento noto/accettato – micap