Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

PhoneyDeveloper

macrumors 68040
Original poster
Sep 2, 2008
3,114
93
This is how I determine system version, hardware version, and other static properties of the runtime environment in my iOS apps.

I have a header and .m file containing the prototypes and code. These are C linkage functions that return the desired info. I do it this way partly for readability and partly for efficiency. I'd rather read (and write) this

Code:
if (isiPad()) {

than the whole long expression to tell if it's an iPad.

My apps call setupCompatibility() one time early during app startup from the app delegate in +initialize.

This is an abbreviated version of my compatibility code and I've recently pruned out some older functions that were no longer used that checked for features that were added to the OS in the iOS 2, and 3 releases. My lowest supported OS these days is 5.0 so there's no need to check for those features before using them.

BTW, the system version number is a string, like @"6.1.3" and I believe that the correct way to compare these version strings is with the NSString numeric search method, as shown in the code.


Code:
//
//  Compatibility.h
//
//  Created by PhoneyDeveloper on 8/20/09.
//

void setupCompatibility();
BOOL CurrentOSVersionIsGreaterOrEqualTo(NSString* inOSVersion);
BOOL isiPad();
BOOL isv6OrGreater();
BOOL isv7OrGreater();

//
//  Compatibility.m
//
//  Created by PhoneyDeveloper on 8/20/09.
//

#import "Compatibility.h"

static BOOL sCompatibilityIsSetup;
static BOOL sIsv6OrGreater;
static BOOL sIsv7OrGreater;
static BOOL sIsIPad;

void setupCompatibility()
{
#ifdef UI_USER_INTERFACE_IDIOM
	sIsIPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
#else
	sIsIPad = NO;
#endif
	
	// Is this OS 6.0 or later
	sIsv6OrGreater = CurrentOSVersionIsGreaterOrEqualTo(@"6.0");
	
	// Is this OS 7.0 or later
	sIsv7OrGreater = CurrentOSVersionIsGreaterOrEqualTo(@"7.0");

	sCompatibilityIsSetup = YES;
}

BOOL isiPad()
{
	NSCAssert(sCompatibilityIsSetup, @"Compatibility Not Setup");
	return sIsIPad;
}

BOOL isv6OrGreater()
{
	NSCAssert(sCompatibilityIsSetup, @"Compatibility Not Setup");
	return sIsv6OrGreater;
}

BOOL isv7OrGreater()
{
	NSCAssert(sCompatibilityIsSetup, @"Compatibility Not Setup");
	return sIsv7OrGreater;
}

// Return true if the current OS version is >= the passed in OS version.
// eg, @"3.1.2" >= @"3.0"

BOOL CurrentOSVersionIsGreaterOrEqualTo(NSString* inOSVersion)
{
	NSString *osVersion = [[UIDevice currentDevice] systemVersion];

	return ([osVersion compare:inOSVersion options:NSNumericSearch] >= NSOrderedSame);
}
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,560
6,059
Thanks for sharing. It's too bad following DROP* is so inconvenient. Tons of great code is in existence but locked away on the authors machine. It will be written and rewritten by many others. Some of them might share it, but many more people will write it again afterwards because they know no better. This is a massive waste of time that I'd like to see solved by 2020, by myself if necessary.

*Don't Repeat Other Programmers. It's a term I believe I coined myself, which was inspired by DRY - Don't Repeat Yourself.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.