How to Get the Platform Name

Dec 8, 2009   //   by Derek van Vliet   //   Development  //  1 Comment

With each iPhone OS-powered device that Apple has added to its lineup, along with it has come new capabilities and new APIs. Because of this, it is often necessary for an app to figure out what platform it is running on before deciding to enable device-specific functionality, such as functionality that would use the compass on the iPhone 3GS, vibration on iPhones or GPS on iPhones.

Here is a function that serves just that purpose by returning the internal name of the platform:

#include <sys/types.h>
#include <sys/sysctl.h>
 
-(NSString*)getPlatformName {
	size_t size;  
	sysctlbyname("hw.machine", NULL, &size, NULL, 0);  
	char *machine = (char*)malloc(size);  
	sysctlbyname("hw.machine", machine, &size, NULL, 0);  
	NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
	free(machine);
 
	return platform;
}

Possible Values

The possible values that the above function will return when developing for the iPhone OS are listed below along with the product they represent.

Return Value Product
i386 iPhone Simulator
x86_64 iPhone Simulator
iPhone1,1 iPhone 1G
iPhone1,2 iPhone 3G
iPhone2,1 iPhone 3GS
iPhone3,1 iPhone ?
iPod1,1 iPod Touch 1G
iPod2,1 iPod Touch 2G

Example Usage

The following is an example of an if statement that takes advantage of the results of the above function.

NSString * platform = [self getPlatformName];
 
if ([platform isEqualToString:@"i386"]) {
	// do simulator-specific stuff, perhaps enable a more verbose level of logging?
}
else if ([platform isEqualToString:@"iPod2,1"]) {		
	// do iPod-specific stuff, perhaps disable UI that refers to GPS, vibration, etc.
}
else if ([platform isEqualToString:@"iPhone2,1"]) {
	// do iPhone 3GS-specific stuff, perhaps compass related?
}

1 Comment

  • [...] to Open the Mail App With a Pre-Composed HTML Email 7. How to Get the Current Time With NSDate 8. How to Get the Platform Name 9. How to Set Your App’s Splash Screen 10. String Comparison Using NSString 11. How to Hide [...]

Leave a comment

Our Games

Latest Tweets