-
-
Save steipete/6526860 to your computer and use it in GitHub Desktop.
// Taken from http://PSPDFKit.com. This snippet is under public domain. | |
#define UIKitVersionNumber_iOS_7_0 0xB57 | |
BOOL PSPDFIsUIKitFlatMode(void) { | |
static BOOL isUIKitFlatMode = NO; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
// We get the modern UIKit if system is running >= iOS 7 and we were linked with >= SDK 7. | |
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0) { | |
isUIKitFlatMode = (NSVersionOfLinkTimeLibrary("UIKit") >> 16) >= UIKitVersionNumber_iOS_7_0; | |
} | |
}); | |
return isUIKitFlatMode; | |
} |
Here's a different variant that most certainly will not pass App Store review, but it's interesting nonetheless: https://gist.github.com/zwaldowski/6526790
If kCFCoreFoundationVersionNumber_iOS_7_0 is undefined, use following snippet:
ifndef kCFCoreFoundationVersionNumber_iOS_7_0
define kCFCoreFoundationVersionNumber_iOS_7_0 847.2
endif
Is there a downside to just making a new UIWindow in all cases? It's not that much overhead is it?
Would it be simpler to use something like this?
isUIKitFlatMode = [UIWindow instancesRespondToSelector:@selector(tintColor)];
Then you don't have to worry about checking the key window or using the temporary window.
@jnjosh UIWindow will always respond to tintColor on iOS 7, even in legacy mode.
@khakionion This is only a fallback and only created if we don't yet have a window, and instantly destroyed afterwards. Wasn't an issue in my test, and no performance penalty either. But it's not pretty, I agree.
How about [[NSBundle mainBundle] objectForInfoDictionaryKey:@"DTSDKName"] ? Xcode injects that key into app's Info.plist during compilation, and it contains the name of Base SDK (e.g. "iphoneos6.1" or "iphonesimulator6.1").
Looks like this doesn't work anymore as of iOS 7.1b2. I've updated the code for a new way that works.
NSVersionOfLinkTimeLibrary seems to be the most stable solution. Are those version numbers defined somewhere, or should I just use them as magic constants?
Check the dyld(3)
man page:
int32_t
NSVersionOfLinkTimeLibrary(const char* libraryName);
NSVersionOfLinkTimeLibrary() returns the current_version number that the main executable was linked
against at build time. The libraryName parameter would be "bar" for /path/libbar.3.dylib and "Foo" for
/path/Foo.framework/Versions/A/Foo. This function returns -1 if the main executable did not link
against the specified library.
They're just whatever number the team for that library uses.
In order to check which SDK version was used to build a binary, UIKit uses the _UIApplicationLinkedOnOrAfter
function. It is implemented by comparing the major version of UIKit at link time (NSVersionOfLinkTimeLibrary("UIKit") >> 16
) to a value in the __UIKitVersionNumbers
table.
Here is this UIKit version numbers table, built by running otool -L
on UIKit of all these iOS versions (yes I have a huge archive!):
Index | iOS Version | Hexadecimal | Decimal |
0 | 2.0 | 0x0E5 | 229 |
1 | 2.1 | 0x2EB | 747 |
2 | 2.2 | 0x2F0 | 752 |
3 | 2.2.1 | 0x2F1 | 753 |
4 | 3.0 | 0x333 | 819 |
5 | 3.1 | 0x3E8 | 1000 |
6 | 3.2 | 0x44C | 1100 |
7 | 4.0 | 0x4B0 | 1200 |
8 | 4.1 | 0x514 | 1300 |
9 | 4.2.1 | 0x578 | 1400 |
10 | 4.2.6 | 0x582 | 1410 |
11 | 4.3 | 0x5DC | 1500 |
12 | 5.0 | 0x640 | 1600 |
13 | 5.1 | 0x6A4 | 1700 |
14 | 6.0 | 0x944 | 2372 |
15 | 6.1 | 0x94C | 2380 |
16 | 7.0 | 0xB57 | 2903 |
17 | 7.1 | 0xB77 | 2935 |
Weirdly enough, the __UIKitVersionNumbers
contains 0x2EC
instead of 0x2EB
for iPhone OS 2.1. But since nobody cares at all about iPhone OS 2 this should not be a problem.
Awesome thread!
One tiny thing to add:
If kCFCoreFoundationVersionNumber_iOS_7_0 is undefined, use following snippet:
ifndef kCFCoreFoundationVersionNumber_iOS_7_0
define kCFCoreFoundationVersionNumber_iOS_7_0 847.2
endif
Instead of the defining the var I would use the &
... like
if (&kCFCoreFoundationVersionNumber_iOS_7_0 &&
kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0)
{ ...
For anyone googling their way here, 8.0 is 0xCF6.
iOS 9.3: 0xDB8
iOS 10b4: 0xE0C
For iOS 12 (GM), it seems that casting to unsigned integer is now required:
(lldb) p (int32_t)NSVersionOfLinkTimeLibrary("UIKit") >> 16
(int32_t) $4 = -4536
(lldb) p (uint32_t)NSVersionOfLinkTimeLibrary("UIKit") >> 16
(uint32_t) $5 = 61000
-> UIKitVersionNumber_iOS_12_0 = 0xEE48
iOS 15.0 is 0x13CB
(lldb) p (uint32_t)NSVersionOfLinkTimeLibrary("UIKit") >> 16
(uint32_t) $2 = 5067
iOS 15.2 is 0x1455
(lldb) p (uint32_t)NSVersionOfLinkTimeLibrary("UIKit") >> 16
(uint32_t) $1 = 5205
@manide I've needed a solution that can detect this at runtime, since it's for http://pspdfkit.com and the binary is precompiled with Xcode 5 and might end up in an app built with Xcode 4 or 5 - and I didn't want to make a separate binary just for that.
If you have full control over your compile environment, your solution is the easier one.