iOS8检查remotenotificationtype的权限

我可以检查用户在iOS8之前是否允许通知(警告)权限:

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

它不是在iOS8上工作,它说:

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

控制台说:

 enabledRemoteNotificationTypes is not supported in iOS 8.0 and later. 

那么我如何才能在iOS 8上检查它?

好吧,我解决了我的问题是这样的:

 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 

我扩大了woheras回答了一些更具活力

  - (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; } 

我没有使用isRegisteredForNotifications运气。 我结束了使用currentUserNotificationSettings

 + (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; } 

试图在这里一石二鸟。 首先,不要检查isRegisteredForRemoteNotifications(),你应该检查currentUserNotificationSettings()。 有了这个,你可以看到是否允许警报,声音等。 其次,似乎iOS版本检查在这里是多余的。 只需检查对象是否响应select器就足够了。 最后,代码示例在Swift中,对于那个请求它的用户: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 } 

下面是如何在Swift 2.0中做Daniels的回答

 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 } } }