13

I am trying to see if this is possible. Looks like setting UICollectionViewFlowLayout's estimatedItemSize doesn't work very good in ios 9. It works perfectly fine ios 10. So i am thinking of implementing sizeForItemAt.. method only for ios 9. Is there anyway to do that using @available ?? Will be really helpful if someone can shed light.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Kesava
  • 1,049
  • 14
  • 22
  • **Swift 3 and 4** [Link to **exact solution** to your question](https://stackoverflow.com/a/42358077/7668778) – alegelos Jul 31 '18 at 09:39
  • Possible duplicate of [Check OS version in Swift?](https://stackoverflow.com/questions/24503001/check-os-version-in-swift) – alegelos Jul 31 '18 at 09:41

3 Answers3

13

I wanted to execute code when the version is lower iOS 11, here's how I did it:

if #available(iOS 11, *) {
  // This is probably empty
} else {
  // This code will only be executed if the version is < iOS 11
}

It's not the cleanest solution, but I couldn't find a better way and it does its job.

Jan Erik Schlorf
  • 2,050
  • 21
  • 36
6

Eh? Do as docs say to do:

@available(iOS, obsoleted: 10.0)

Or

@available(iOS, introduced: 9.0, obsoleted: 10.0)
user28434'mstep
  • 6,290
  • 2
  • 20
  • 35
  • 5
    I tried both the options, in both cases, it gets executed in ios 10 as well. Not sure why – Kesava Nov 22 '16 at 12:08
3
let systemVersion = UIDevice.currentDevice().systemVersion
println("iOS\(systemVersion)")

if systemVersion == 9.0 {
  //Do Something
}

You can use like below:

@available(iOS 10.0, *)
private func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    logParklee.verbose("willPresentNotification")
}

Or

if #available(iOS 10, *) {
    // use UIStackView
} else {
    // show sad face emoji
}

Or

guard #available(iOS 9, *) else {
    return
}

Or

@available(iOS 7, *)
func iOS7Work() {
    // do stuff

    if #available(iOS 8, *) {
        iOS8Work()
    }
}

@available(iOS 8, *)
func iOS8Work() {
    // do stuff
    if #available(iOS 9, *) {
        iOS9Work()
    }
}

@available(iOS 9, *)
func iOS9Work() {
    // do stuff
}
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
  • 3
    As i said, I want the entire method to be run only on iOS 9 and not on iOS10. Your solution works the other way around. – Kesava Nov 22 '16 at 11:21
  • Updated my answer. – Parth Adroja Nov 22 '16 at 11:24
  • Again. I want the entire func to be executed on iOS 9 and not on 10. Your version talks only about #available and and not @available. If you see my question, its about sizeForItemAt getting called or not. – Kesava Nov 22 '16 at 11:29
  • @Kesava First code block seems to be perfect and will work only in iOS 9. – BaSha Nov 22 '16 at 11:43
  • 1
    First block works if we are inside a func. I am talking about @available which is for the whole func. not for a piece of code. Please read the question fully. – Kesava Nov 22 '16 at 12:00