iOS8 vérifier l'autorisation de remotenotificationtype

Je peux vérifier si l'user a reçu l'autorisation de notification (alerte) ou pas avant iOS8 comme ça:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types & UIRemoteNotificationTypeAlert) { //user granted } 

il ne fonctionne pas sur iOS8, il dit:

 iOS (3.0 and later) Deprecated:Register for user notification settings using the registerUserNotificationSettings: method instead. 

console dit:

 enabledRemoteNotificationTypes is not supported in iOS 8.0 and later. 

alors comment puis-je le vérifier sur iOS 8?

Ok j'ai résolu mon problème comme ceci:

 BOOL isgranted = false; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) { isgranted = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; } #else UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types & UIRemoteNotificationTypeAlert) { isgranted = true; } #endif 

J'ai développé sur woheras répondre un peu pour être plus dynamic

  - (BOOL)pushNotificationsEnabled { BOOL isgranted = false; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){ if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) { isgranted = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; }else{ } }else{ UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types & UIRemoteNotificationTypeAlert) { isgranted = true; }else{ } } return isgranted; } 

Je n'ai pas eu beaucoup de chance en utilisant isRegisteredForNotifications . J'ai fini par utiliser currentUserNotificationSettings place.

 + (BOOL)notificationServicesEnabled { BOOL isEnabled = NO; if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) { isEnabled = NO; } else { isEnabled = YES; } } else { UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types & UIRemoteNotificationTypeAlert) { isEnabled = YES; } else{ isEnabled = NO; } } return isEnabled; } 

Essayer de tirer deux oiseaux avec une pierre ici. Tout d'abord, au lieu de vérifier isRegisteredForRemoteNotifications (), vous devriez probablement vérifier currentUserNotificationSettings (). Avec cela, vous pouvez voir si les alertes, le son, etc. sont autorisés. Deuxièmement, il semble que la vérification de la version iOS soit redondante ici. Il suffit de vérifier si l'object répond au sélecteur. Enfin, l'exemple de code est dans Swift, pour cet user qui l'a demandé: P

 func hasPushNotificationsEnabled() -> Bool { if UIApplication.sharedApplication().respondsToSelector("currentUserNotificationSettings") { let settings = UIApplication.sharedApplication().currentUserNotificationSettings() return (settings.types & UIUserNotificationType.Alert) != UIUserNotificationType.None } let types = UIApplication.sharedApplication().enabledRemoteNotificationTypes() return (types & UIRemoteNotificationType.Alert) != UIRemoteNotificationType.None } 

Voici comment faire Daniels répondre dans Swift 2.0

 func hasPushEnabled() -> Bool { //ios 8+ if UIApplication.sharedApplication().respondsToSelector("currentUserNotificationSettings") == true { let settings = UIApplication.sharedApplication().currentUserNotificationSettings() if (settings?.types.contains(.Alert) == true){ return true } else { return false } } else { let types = UIApplication.sharedApplication().enabledRemoteNotificationTypes() if types.contains(.Alert) == true { return true } else { return false } } }