sysctl()和sysctlbyname()允许获取系统信息。这些标准的UNIX函数用于询问操作系统有关硬件和OS的详细信息。
这些常量使你能够检查核心信息,比如系统的CPU频率,可用的内存量等。它引入了一个UIDevice类,用于搜集系统信息,并通过一系列方法调用返回它。
每个型号都提供了独特的内置能力,准确知道你正在处理哪款iPhone有助于确定那个单元是否有可能支持诸如可访问性,GPS和磁力计之类的特性。
#pragma mark sysctlbyname utils - (NSString *) getSysInfoByName:(char *)typeSpecifier { 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 [self getSysInfoByName:"hw.machine"]; } // Thanks, Tom Harrington (Atomicbird) - (NSString *) hwmodel { return [self getSysInfoByName:"hw.model"]; }
返回的是:
2014-12-22 15:59:40.391 HelloWorld[2673:943598] Platform: iPhone5,4 2014-12-22 15:59:40.393 HelloWorld[2673:943598] HWModel: N49AP
#pragma mark sysctl utils - (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 [self getSysInfo:HW_CPU_FREQ]; } - (NSUInteger) busFrequency { return [self getSysInfo:HW_BUS_FREQ]; } - (NSUInteger) cpuCount { return [self getSysInfo:HW_NCPU]; } - (NSUInteger) totalMemory { return [self getSysInfo:HW_PHYSMEM]; } - (NSUInteger) userMemory { return [self getSysInfo:HW_USERMEM]; } - (NSUInteger) maxSocketBufferSize { return [self getSysInfo:KIPC_MAXSOCKBUF]; }
查看磁盘空间
- (NSNumber *) totalDiskSpace { NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil]; return [fattributes objectForKey:NSFileSystemSize]; } - (NSNumber *) freeDiskSpace { NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil]; return [fattributes objectForKey:NSFileSystemFreeSize]; }
转载于:https://www.cnblogs.com/haibosoft/p/4178434.html