- Accessing Basic Device Information
- Adding Device Capability Restrictions
- Recipe: Checking Device Proximity and Battery States
- Recipe: Recovering Additional Device Information
- Recipe: Using Acceleration to Locate "Up"
- Working with Basic Orientation
- Retrieving the Current Accelerometer Angle Synchronously
- Recipe: Using Acceleration to Move Onscreen Objects
- Recipe: Accelerometer-Based Scroll View
- Recipe: Core Motion Basics
- Recipe: Retrieving and Using Device Attitude
- Detecting Shakes Using Motion Events
- Recipe: Using External Screens
- Tracking Users
- One More Thing: Checking for Available Disk Space
- Summary
Recipe: Recovering Additional Device Information
Both sysctl() and sysctlbyname() enable you to retrieve system information. These standard UNIX functions query the operating system about hardware and OS details. You can get a sense of the kind of scope on offer by glancing at the /usr/include/sys/sysctl.h include file on the Macintosh. There you can find an exhaustive list of constants that can be used as parameters to these functions.
These constants enable you to check for core information such as the system’s CPU frequency, the amount of available memory, and more. Recipe 1-2 demonstrates this functionality. It introduces a UIDevice category that gathers system information and returns it via a series of method calls.
You might wonder why this category includes a platform method, when the standard UIDevice class returns device models on demand. The answer lies in distinguishing different types of units.
An iPhone 3GS’s model is simply “iPhone,” as is the model of an iPhone 4S. In contrast, this recipe returns a platform value of “iPhone2,1” for the 3GS and “iPhone 4,1” for the iPhone 4S. This enables you to programmatically differentiate the 3GS unit from a first-generation iPhone (“iPhone1,1”) or iPhone 3G (“iPhone1,2”).
Each model offers distinct built-in capabilities. Knowing exactly which iPhone you’re dealing with helps you determine whether that unit likely supports features such as accessibility, GPS, and magnetometers.
Recipe 1-2. Extending Device Information Gathering
@implementation UIDevice (Hardware) + (NSString *) getSysInfoByName:(char *)typeSpecifier { // Recover sysctl information by name size_t size; sysctlbyname(typeSpecifier, NULL, &size, NULL, 0); char *answer = malloc(size); sysctlbyname(typeSpecifier, answer, &size, NULL, 0); NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding]; free(answer); return results; } - (NSString *) platform { return [UIDevice getSysInfoByName:"hw.machine"]; } - (NSUInteger) getSysInfo: (uint) typeSpecifier { size_t size = sizeof(int); int results; int mib[2] = {CTL_HW, typeSpecifier}; sysctl(mib, 2, &results, &size, NULL, 0); return (NSUInteger) results; } - (NSUInteger) cpuFrequency { return [UIDevice getSysInfo:HW_CPU_FREQ]; } - (NSUInteger) busFrequency { return [UIDevice getSysInfo:HW_BUS_FREQ]; } - (NSUInteger) totalMemory { return [UIDevice getSysInfo:HW_PHYSMEM]; } - (NSUInteger) userMemory { return [UIDevice getSysInfo:HW_USERMEM]; } - (NSUInteger) maxSocketBufferSize { return [UIDevice getSysInfo:KIPC_MAXSOCKBUF]; } @end