检查是否在 iOS7 上启用了推送通知

Check if push notifications are enabled on iOS7

我应用的一些用户仍在使用 iOS7。 对于 iOS 8 及更高版本,我只需使用:

UIApplication.sharedApplication().currentUserNotificationSettings()!.types

或:

UIApplication.respondsToSelector(#selector(UIApplication.isRegisteredForRemoteNotifications))

但是除了 iOS7 之外,我该用什么来检查通知是否已启用?

提前致谢!

这里问了一个类似的问题:Push Notification ON or OFF Checking in iOS(在 obj-c 中)

我为您将其转换为 swift:

//if iOS 7
var types: UIRemoteNotificationType = UIApplication.sharedApplication().enabledRemoteNotificationTypes()
if types != .None 
{
    print(" Push Notification ON")
} 

编辑:您还可以通过执行以下操作来检查正在使用哪个 ios:

var iOSversion: String = UIDevice.currentDevice().systemVersion()
var prefix: String = iOSversion.componentsSeparatedByString(".").first!
var versionVal: Float = CFloat(prefix)!

if versionVal >= 8 {
    if UIApplication.sharedApplication().currentUserNotificationSettings().types != .None {
         print(" Push Notification ON")
    }
    else {
         var msg: String = "Please press ON to enable Push Notification"
         var alert_push: UIAlertView = UIAlertView(title: "Push Notification Service Disable", message: msg, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Setting")
         alert_push.tag = 2
         alert_push.show()
         print(" Push Notification OFF")
    }
}

else {
   var types: UIRemoteNotificationType = UIApplication.sharedApplication().enabledRemoteNotificationTypes()
   if types != .None {
       print(" Push Notification ON")
   }
   else {
      var msg: String = "Please press ON to enable Push Notification"
      var alert_push: UIAlertView = UIAlertView(title: "Push Notification Service Disable", message: msg, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Setting")
      alert_push.tag = 2
      alert_push.show()
      print(" Push Notification OFF")
}