1

I would like to have a macro that allows me to check the version or higher than the ios 13. I don't want to use that :

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

Because the preprocessor will try to compile it and will not be able to do it. What I want is a macro that allows to compile properly without warning or errors message. Something like that:

#ifdef __IPHONE_13_0

But I am not sure that line means 13.0 or higher.

My code in the .h is the following:

#ifdef __AVAILABILITY_INTERNAL__IPHONE_13_0
@property (nonatomic, retain) UIViewController *popoverController;
#else
@property (nonatomic, retain) UIPopoverController *popoverController;
#endif

It doesn't work. How to check in the .h file the version of the ios? I cannot use that as it is in the .h file :

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
    // code here
}

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
    // code here
}

What do you think ?

EDIT: How to avoid to get the message for the ipads before a previous version?

enter image description here Thanks in advance.

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • Does this answer your question? [How can we programmatically detect which iOS version is device running on?](https://stackoverflow.com/questions/7848766/how-can-we-programmatically-detect-which-ios-version-is-device-running-on) – Larme Jul 15 '20 at 13:37
  • @Larme I already check that ticket but the problem is that it is not macros, it is code. The preprocessor will try to compile the code event if the version is lower and even if it won't be used. – ΩlostA Jul 15 '20 at 13:41
  • 1
    No, it's not possible to find out at compile time on which iOS version the code will be running. – Andreas Oetjen Jul 15 '20 at 13:53
  • @OlSen I was talking about compile time, not run time. And, the title of the question reads "the version", so I thought the author meant "the" runtime version, not the target version at compile time. – Andreas Oetjen Jul 16 '20 at 04:36

1 Answers1

2

But you are allowed to use the preprocessor variables when defining your API

@property (nonatomic, retain) UIViewController *viewController NS_DEPRECATED_IOS(2.0, 13.0);
@property (nonatomic, retain) UIPopoverController *popoverController API_AVAILABLE(ios(13.0));

same for methods

-(void)halleluja:(Evil)brain API_AVAILABLE(ios(13.0)) {
    //do evil stuff with brain
}

do not forget, your Apps targeted system version is meant here.

You will find plenty of different variations on how to in the documentations of (upper file part)
#import "Availability.h"
But you don't need to include it, Xcode does that for you.

You can also tell your pre-compiler

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_13
    // code starting with ios(13.0)
    [self halleluja:nope];
#else
    // code earlier ios(13.0)
    [self halleluja:papa]; //as defined above should throw a warning
#endif 

and finally you can ask at runtime

if ([[[UIDevice currentDevice] systemVersion] isEqualToString:@"13.0"]) {
    // code only on ios(13.0) and only ios(13.0)!
}

or

NSString *versionString = [[UIDevice currentDevice] systemVersion];
if ([versionString floatValue] >= 13.0) {
    // not sexy - but works.
}
Ol Sen
  • 3,163
  • 2
  • 21
  • 30
  • Perfect that's it. When I compilate for an ios12 I can use a specific code and for ios13 another specific code with no deprecated warning. Thanks. – ΩlostA Jul 15 '20 at 17:34
  • But I still have an error message of deprecated in the .m file, can you check my ticket, I added it. – ΩlostA Jul 15 '20 at 17:51
  • 1
    Yes you will have a warning which is good because you don't want to code for a version without knowledge that parts are deprecated. It keeps your mind fresh about the fact that you have to be aware of your coding patterns. You can silent deprecated warnings in Build Settings for your Scheme when using clang as compiler and for sure the others too. – Ol Sen Jul 15 '20 at 20:03