Ios7 在swift中注册iOS 7通知

Ios7 在swift中注册iOS 7通知,ios7,notifications,xcode6,Ios7,Notifications,Xcode6,我希望能够在iOS 7和iOS 8上进行通知,我已经成功地使用iOS 8进行了设置,但是在iOS 7上,我只收到一个错误“lldb”,在“var mySettings…”行上没有其他错误。从我所读到的内容来看,这就是为什么你要在iOS 7上注册它,但它似乎不起作用 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)

我希望能够在iOS 7和iOS 8上进行通知,我已经成功地使用iOS 8进行了设置,但是在iOS 7上,我只收到一个错误“lldb”,在“var mySettings…”行上没有其他错误。从我所读到的内容来看,这就是为什么你要在iOS 7上注册它,但它似乎不起作用

   func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> bool{

    //check if its on ios8

    var deviceVersion :  NSString = UIDevice.currentDevice().systemVersion



    if deviceVersion.floatValue >= 8.0 {

     //I've set up the iOS 8 notifications here and that all works.

    }else{



        var types : UIUserNotificationType = UIUserNotificationType.Badge | UIUserNotificationType.Alert;



        var mySettings : UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)



        UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)



    }





    return true

}
你的问题是:

UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)

这在ios 7中不受支持。您应该实现如下内容:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// Override point for customization after application launch.
if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:")))
{
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
}
else
{
   //do ios7 stuff here. If you are using just local notifications then you dont need to do anything. for remote notifications:
application.registerForRemoteNotificationTypes(UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge)
}
return true
}