iOS: Preprocessor for OS version check - ios

In the past I used the following preprocessor code to conditionally execute code for different iOS versions:
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
// target is iOS
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000
// target is lower than iOS 6.0
#else
// target is at least iOS 6.0
#endif
#endif
However with iOS 7 I have the following problem:
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
// target is iOS
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000
// target is lower than iOS 7.0
NSLog(#"This message should only appear if iOS version is 6.x or lower");
#else
// target is at least iOS 7.0
#endif
#endif
The NSLog message above appears on console under iOS 7. Am I doing something wrong?
EDIT: The following code running under iOS 7 (simulator and device)
NSLog(#"Version %i", __IPHONE_OS_VERSION_MIN_REQUIRED);
gives: Version 60000

That is the Deployment Target of your app (the minimum version where your app can be installed), not the version where the app is running in the device.
In the settings of your project, you can set that field:
If you change it like this, this input:
NSLog(#"Version %i", __IPHONE_OS_VERSION_MIN_REQUIRED);
Returns 7000
If what you want is to check the actual version of the operative system, I refer you to this question:
How to check iOS version?
But, it's done in runtime, not at compile time.

#ifdef __AVAILABILITY_INTERNAL__IPHONE_9_0_DEP__IPHONE_9_0
// you're in Xcode 7.x and can use buggy SDK with ios 9.0 only functionality
CGFloat iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (iOSVersion>=9) {
// same good old API that predates brave new post Steve Jobs world of bugs and crashes
}
#else
// you're running Xcode 6.4 or older and should use older API here
#endif
swift:
if #available(iOS 13, *) {
toSearchBar?.isHidden = true
} else {
// a path way to discovering how fast UIKit will rot
// now that there is SwiftUI
}

Related

Unknown type name 'SKPaymentDiscount'. FBSDKCoreKit

I am facing an issue when building the Facebook sdk for iOS facebook-ios-sdk in my Swift 3 iOS application.
I have recently updated the FBSDKCorekit and FBSDKLoginkit pods I'm using in my iOS application from version 4.30.0 to 5.13.1
I am using XCode 9.3.1 targeting iOS 10.2. when building my application with XCode I get the following issue: Unknown type name 'SKPaymentDiscount'; did you mean 'SKProductDiscount'?
This error is raised when the compiler attempts to build the following file Core/FBSDKPaymentObserver.m. See the following segment.
...
#if !TARGET_OS_TV
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_11_1
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_11_4
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_12_1
// promotional offer starting from iOS 12.2
if (#available(iOS 12.2, *)) {
SKPaymentDiscount *paymentDiscount = transaction.payment.paymentDiscount;
if (paymentDiscount) {
NSArray<SKProductDiscount *> *discounts = product.discounts;
for (SKProductDiscount *discount in discounts) {
if (discount.paymentMode == SKProductDiscountPaymentModeFreeTrial &&
[paymentDiscount.identifier isEqualToString:discount.identifier]) {
return YES;
}
}
}
}
#endif
...
As I understood after doing some research, SKPaymentDiscount was only introduced in iOS sdk 12.2 . Since I am using XCode 9.3.1 the latest available iOS SDK is 11.3 but the conditional compiler expression #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_12_1
should make sure the compiler does not build the previous code segment.
However that doesn't seem to be the case and I am getting the build error.
Unknown type name 'SKPaymentDiscount'; did you mean 'SKProductDiscount'?
I have tried installing XCode 10.0 which supports iOS sdk 12.2 but I got the same error. Please advise.
You should update to Xcode 11.3.1. It is the latest version currently as of Jan 2020.

How can I conditionally compile code for Catalyst?

I'm working on porting an iOS application to Catalyst. The Catalyst (Mac) version will have its own target.
Is there an official way to conditionally compile code just for Catalyst? Otherwise, I can add a target-specific define, but it would be better to use something more general.
As seen in the documentation Creating a Mac Version of Your iPad App, you do:
Swift:
#if targetEnvironment(macCatalyst)
// Code specific to Mac.
#else
// Code to exclude from Mac.
#endif
Objective-C:
#if TARGET_OS_MACCATALYST
// Code specific to Mac.
#else
// Code to exclude from Mac.
#endif
It is also possible to run code that is excluded from macCatalyst without having to use the #else. Note the use of ! (not).
#if !targetEnvironment(macCatalyst)
print("This code will not run on macCatalyst")
#endif

Conditionally Hide Code from the Compiler

So here's the problem. I'm set to release an update soon for iOS that will address some problems in iOS 7. In order to do this, I need to use some specific iOS 7 functions/types. I've made absolutely certain that iOS 7 code will only be executed on iOS 7 and fallback to different code for pre iOS 7. Of course, I'm not allowed to submit with the current Xcode beta, so I'm trying to compile with the current Xcode release. However, I can't seem to find a way to disable this particular warning:
Use of undeclared identifier '<Redacted>'.
Does anyone know of a way to disable this warning using a #pragma. I've tried a bunch of different ones including
-w, -Weverthing, -Wall
but nothing seems to work.
UPDATE
Answer: You can't, of course, because the compiler can't compile an identifier it knows nothing about. My solutions was to simply create a #define:
#define <redacted> 1
UPDATE 2
The answer below actually made it much easier. I had already created a #define Xcode5Code(code, alt) that allowed me to execute code blocks conditionally. By modifying it using the solution by #maddy:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
#define Xcode5Code(code, alt) code
#else
#define Xcode5Code(code, alt) alt
#endif
This allows me to to easily hide blocks of code from the compiler by using:
Xcode5Code({
//Code to be execute only with Xcode 5
}, {
//code to be executed in previous versions of Xcode
})
The main benefit of using the #define Xcode5Code is that Xcode will auto-complete it for you, which is a lot easier than using the full #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000, which Xcode won't auto-complete.
This won't actually distinguish between iOS 7 and pre iOS 7 devices. It only distinguishes what version of iOS the current Xcode can handle. To distinguish between iOS devices versions I use:
NSUInteger DeviceSystemMajorVersion(void) {
static NSUInteger _deviceSystemMajorVersion = -1;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:#"."] objectAtIndex:0] intValue];
});
return _deviceSystemMajorVersion;
}
The above is Apple's code, by the way. To dance around the NDA a little, I'll say that this helps with laying out a root controller's view, because that depends on both the version of Xcode you're using AND the version of iOS that's on the device. And if you're trying to manage beta's as well as production code, this can help a lot. Once you can submit apps with Xcode 5, the #define Xcode5Code will no longer be necessary.
If you want to compile your app with two difference versions of Xcode or two different Base SDK settings then you should use compiler directives:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 // iOS 7.0 supported
// iOS 7 code here
#else
// Pre-iOS 7 code here
#endif
Do not use this for runtime checks. This solution is only to be used when you must compile your code with two different versions. An example would be you have added iOS 7 code but you still need to compile the code with Xcode 4.6. Using the compile directives allows you to "hide" the iOS 7 code from the compiler using the older Base SDK.
See the "SDK Compatibility Guide" in the docs for more on this and proper runtime checks.

Is there a way to find methods in my code which won't run on previous versions of iOS?

I had a problem with the latest release of my app, where iOS 5 users said it was crashing. I quickly discovered the problem to be with using an iOS 6 method. I didn't realise this method was a new one. I was wondering if there was a way to quickly check my code (without doing it method-by-method) to make sure all code is compatible with previous versions of iOS?
You should test your app on iPhone/iPad 5.0 simulator. I think they are not available by default XCode 4.6 onwards, but you can download them from: Preferences > Downloads > Components.
Edit: It seems like XCode doesn't warn about new APIs in your code. There a workaround described in this answer: Get xcode 4.5 to warn about new API calls -
#define __AVAILABILITY_TOO_NEW __attribute__((deprecated("TOO NEW!"))) __attribute__((weak_import))
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
#undef __AVAILABILITY_INTERNAL__IPHONE_6_0
#define __AVAILABILITY_INTERNAL__IPHONE_6_0 __AVAILABILITY_TOO_NEW
#endif

Only run code if iOS < 6.0 with preprocessing?

I can find many examples on how to only compile/run code then iOS version is > something, but how do I do it the other way around? I tried the following by running iOS 5.0 in the simulator:
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_6_0
// Code for iOS < 6.0 here
#endif
But the code inside the #if - #endif is not run on iOS 5.0 in the simulator. How can I do this?
[EDIT]
Ok so I wasn't sure what I wanted it seems, sorry :) The thing is that I want this code in my UITableViewDelegate to be run only if the device is running iOS < 6.0:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
}
This is because I want to do some styling if I am running iOS < 6, but on iOS 6 I can do this styling much much easier. But a version check at runtime inside this method is not really what I want because then it is to late.
Thank you
Søren
There's a difference between
a compile-time check if you're compiling against a specific SDK and
checking which OS your code is running on
Checking for the __IPHONE_6_0 macro will just check which target you're compiling for... is that what you want? If so, you could use #ifndef __IPHONE_6_0 to check if you are not compiling for iOS 6.
If you want to know which OS your code is running on, you can check MSK's answer.
Here is a run time not compile time check.
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
use it like
if(SYSTEM_VERSION_LESS_THAN(#"6.0"))
{
}

Resources