swift, write code for ios 7 and 8

后端 未结 5 767
悲&欢浪女
悲&欢浪女 2020-12-30 03:23

i got an test app I\'m writing with Swift, I want to target iOS 7. But enable local notification I need to add

application.registerUserNotificationSettings(         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 03:38

    There is a very good answer by Prasath, but it is written in Objective-C,
    so I have written something similar in swift:

    (tested in Xcode 6 )

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
       // ...
    
       // Set Notification
    
       if UIApplication.sharedApplication().respondsToSelector(Selector("registerUserNotificationSettings:")) {
    
          // Notifications for iOS 8
          let notificationSettings = UIUserNotificationSettings(forTypes: .Alert | .Sound, categories: nil)
          UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
       }
       else {
          // Notifications for iOS < 8
          UIApplication.sharedApplication().registerForRemoteNotificationTypes(.Alert | .Sound)
       }
    
       // ...
    
       return true
    }
    

    Hope that helps

提交回复
热议问题