0

I am writing objective-c code in Xcode 8.0, for API availability I am using

 if (@available(iOS 11, *)) {
        // iOS 11 (or newer) ObjC code
 } else {
        // iOS 10 or older code
 }

Unfortunately compiler fires:

unrecognised platform name iOS

Why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Amit
  • 556
  • 1
  • 7
  • 24
  • iOS 11 support was added in Xcode **9** – Martin R Oct 25 '17 at 04:44
  • @MartinR if i write `if (@available(iOS 9, *))`,8,7 gives me same. – Amit Oct 25 '17 at 04:46
  • 1
    @Amit, it does not matter if you check for versions below iOS 11. You need to have Xcode 9 to be able to make the `@available` check. – Rashwan L Oct 25 '17 at 04:48
  • @RashwanL how can i apply multiple checks for diffrent version of iOS ? – Amit Oct 25 '17 at 04:51
  • Just extend and add more conditions to your if-statement. – Rashwan L Oct 25 '17 at 04:54
  • @RashwanL let me explain you more. Some api works with `iOS8`, some with`iOS9`, So how can i apply checks with **Xcode8** , does `@available` or something else to use ? – Amit Oct 25 '17 at 04:58
  • @Amit, you can´t make this checks with Xcode 8. The `@availability` syntax are **only** available in Xcode 9. Upgrade your Xcode to version 9 and you´ll be able to check for iOS 8, 9, 10, 11 etc. Checkout my answer below it describe this. – Rashwan L Oct 25 '17 at 05:00
  • Strange `if #available(iOS 10.0, *) {` is working in **Xcode8**, **Swift** – Amit Oct 25 '17 at 05:08
  • @Amit Objective-C and Swift are different languages. The `@available` syntax was added to a new version of Objective-C which is available only through Xcode 9, as others have mentioned. It doesn't matter what OS version you're _targeting_ — what matters is the version of the compiler you're _compiling with_, which is what has changed between Xcode 8 and 9. – Itai Ferber Oct 25 '17 at 05:17
  • @ItaiFerber true! this is correct answer of my question. – Amit Oct 27 '17 at 04:20

1 Answers1

5

As I wrote at How to check iOS version?:

  • @available was added in Xcode 9
  • #available was added in Xcode 7

If you need to test availability for multiple versions of Xcode, you can detect Xcode 9 with #ifdef __MAC_10_13 or, better, #if __clang_major__ >= 9, as below:

#if __clang_major__ >= 9
    // Xcode 9+
    if (@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)) {
        // iOS 8+
    }
#else
    // Xcode 8-
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber10_9_2) {
        // iOS 8+
    }
#endif
    else {
        // iOS 7-
    }

Real case example taken from: https://github.com/ZipArchive/ZipArchive/blob/master/SSZipArchive/SSZipArchive.m

Cœur
  • 37,241
  • 25
  • 195
  • 267