swift, write code for ios 7 and 8

后端 未结 5 740
悲&欢浪女
悲&欢浪女 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:34

    I ran into a similar issue, so for anyone having this, here is another possible solution:

    If you go to your Target > General > Linked Frameworks and Libraries, you should see UIKit (if not, add it). Make sure it says "Optional" instead of "Required" next to it...

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-30 03:42

    Unfortunately, I think you are running into a current limitation, see this

    What is the Swift preprocessor equivalent to iOS version check comparison?

    The only way to get around this is to add an Objective-C file, and then use #if macros and make two helper functions that you call from Swift (one for iOS8 and one for iOS7). I expect this will be fixed at some point.

    0 讨论(0)
  • 2020-12-30 03:45

    This is kind of a dirty hack but it works for me:

    if(UIApplication.sharedApplication().respondsToSelector("isRegisteredForRemoteNotifications")){ println("is iOS8") }
    
    0 讨论(0)
  • 2020-12-30 04:00

    In Beta6 it appears that some (all?) of the linking problems have been fixed. Apps linked with UIAlertAction and UIAlertController used to fail to launch on 7.1, but now will launch. You still cannot use them in 7.1, but you can test for iOS version and use the older objects.

    0 讨论(0)
提交回复
热议问题