GBDeviceInfo_OSX.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // GBDeviceInfo_OSX.m
  3. // GBDeviceInfo
  4. //
  5. // Created by Luka Mirosevic on 14/03/2013.
  6. // Copyright (c) 2013 Goonbee. All rights reserved.
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License");
  9. // you may not use this file except in compliance with the License.
  10. // You may obtain a copy of the License at
  11. //
  12. // http://www.apache.org/licenses/LICENSE-2.0
  13. //
  14. // Unless required by applicable law or agreed to in writing, software
  15. // distributed under the License is distributed on an "AS IS" BASIS,
  16. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. // See the License for the specific language governing permissions and
  18. // limitations under the License.
  19. #import "GBDeviceInfo_OSX.h"
  20. #import <sys/utsname.h>
  21. #import "GBDeviceInfo_Common.h"
  22. #import "GBDeviceInfo_Subclass.h"
  23. static NSString * const kHardwareModelKey = @"hw.model";
  24. @interface GBDeviceInfo ()
  25. @property (assign, atomic, readwrite) GBDeviceVersion deviceVersion;
  26. @property (assign, atomic, readwrite) GBByteOrder systemByteOrder;
  27. @property (assign, atomic, readwrite) BOOL isMacAppStoreAvailable;
  28. @property (assign, atomic, readwrite) BOOL isIAPAvailable;
  29. @end
  30. @implementation GBDeviceInfo
  31. - (NSString *)description {
  32. return [NSString stringWithFormat:@"%@\nrawSystemInfoString: %@\nnodeName: %@\nfamily: %ld\ndeviceModel.major: %ld\ndeviceModel.minor: %ld\ncpuInfo.frequency: %.3f\ncpuInfo.numberOfCores: %ld\ncpuInfo.l2CacheSize: %.3f\npysicalMemory: %.3f\nsystemByteOrder: %ld\nscreenResolution: %.0fx%.0f\nosVersion.major: %ld\nosVersion.minor: %ld\nosVersion.patch: %ld",
  33. [super description],
  34. self.rawSystemInfoString,
  35. self.nodeName,
  36. self.family,
  37. (unsigned long)self.deviceVersion.major,
  38. (unsigned long)self.deviceVersion.minor,
  39. self.cpuInfo.frequency,
  40. (unsigned long)self.cpuInfo.numberOfCores,
  41. self.cpuInfo.l2CacheSize,
  42. self.physicalMemory,
  43. self.systemByteOrder,
  44. // self.displayInfo.resolution.width,
  45. // self.displayInfo.resolution.height,
  46. (unsigned long)self.osVersion.major,
  47. (unsigned long)self.osVersion.minor,
  48. (unsigned long)self.osVersion.patch
  49. ];
  50. }
  51. #pragma mark - Public API
  52. - (instancetype)init {
  53. if (self = [super init]) {
  54. self.rawSystemInfoString = [self.class _rawSystemInfoString];
  55. self.family = [self.class _deviceFamily];
  56. self.physicalMemory = [self.class _physicalMemory];
  57. self.systemByteOrder = [self.class _systemByteOrder];
  58. self.osVersion = [self.class _osVersion];
  59. self.deviceVersion = [self.class _deviceVersion];
  60. self.isMacAppStoreAvailable = [self.class _isMacAppStoreAvailable];
  61. self.isIAPAvailable = [self.class _isIAPAvailable];
  62. }
  63. return self;
  64. }
  65. #pragma mark - Dynamic Properties
  66. - (NSString *)nodeName {
  67. return [self.class _nodeName];
  68. }
  69. - (GBCPUInfo)cpuInfo {
  70. return [self.class _cpuInfo];
  71. }
  72. //- (GBDisplayInfo)displayInfo {
  73. // return [self.class _displayInfo];
  74. //}
  75. #pragma mark - Private API
  76. + (struct utsname)_unameStruct {
  77. struct utsname systemInfo;
  78. uname(&systemInfo);
  79. return systemInfo;
  80. }
  81. + (GBOSVersion)_osVersion {
  82. GBOSVersion osVersion;
  83. if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) {
  84. NSOperatingSystemVersion osSystemVersion = [[NSProcessInfo processInfo] operatingSystemVersion];
  85. osVersion.major = osSystemVersion.majorVersion;
  86. osVersion.minor = osSystemVersion.minorVersion;
  87. osVersion.patch = osSystemVersion.patchVersion;
  88. }
  89. else {
  90. SInt32 majorVersion, minorVersion, patchVersion;
  91. #pragma clang diagnostic push
  92. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  93. Gestalt(gestaltSystemVersionMajor, &majorVersion);
  94. Gestalt(gestaltSystemVersionMinor, &minorVersion);
  95. Gestalt(gestaltSystemVersionBugFix, &patchVersion);
  96. #pragma clang diagnostic pop
  97. osVersion.major = majorVersion;
  98. osVersion.minor = minorVersion;
  99. osVersion.patch = patchVersion;
  100. }
  101. return osVersion;
  102. }
  103. //+ (GBDisplayInfo)_displayInfo {
  104. // CGSize displaySize = CGDisplayScreenSize(kCGDirectMainDisplay); // CGMainDisplayID()
  105. // CGFloat displayAreaMm = displaySize.width * displaySize.height;
  106. //
  107. // NSScreen *mainScreen = [NSScreen mainScreen];
  108. // CGFloat width = (CGFloat)CGDisplayPixelsWide(kCGDirectMainDisplay) * mainScreen.backingScaleFactor;
  109. // CGFloat height = (CGFloat)CGDisplayPixelsHigh(kCGDirectMainDisplay) * mainScreen.backingScaleFactor;
  110. // CGFloat pixelCount = width * height;
  111. //
  112. // CGFloat pixelsPerMm = sqrt(pixelCount / displayAreaMm);
  113. // CGFloat pixelsPerInch = pixelsPerMm * 25.4;
  114. //
  115. // return GBDisplayInfoMake(
  116. // mainScreen.frame.size, pixelsPerInch
  117. // );
  118. //}
  119. + (GBDeviceFamily)_deviceFamily {
  120. NSString *systemInfoString = [self _rawSystemInfoString];
  121. if ([systemInfoString hasPrefix:@"iMac"]) {
  122. return GBDeviceFamilyiMac;
  123. }
  124. else if ([systemInfoString hasPrefix:@"Mac Mini"]) {
  125. return GBDeviceFamilyMacMini;
  126. }
  127. else if ([systemInfoString hasPrefix:@"Mac Pro"]) {
  128. return GBDeviceFamilyMacPro;
  129. }
  130. else if ([systemInfoString hasPrefix:@"MacBook Pro"]) {
  131. return GBDeviceFamilyMacBookPro;
  132. }
  133. else if ([systemInfoString hasPrefix:@"MacBook Air"]) {
  134. return GBDeviceFamilyMacBookAir;
  135. }
  136. else if ([systemInfoString hasPrefix:@"MacBook"]) {
  137. return GBDeviceFamilyMacBook;
  138. }
  139. else if ([systemInfoString hasPrefix:@"Xserve"]) {
  140. return GBDeviceFamilyXserve;
  141. }
  142. else {
  143. return GBDeviceFamilyUnknown;
  144. }
  145. }
  146. + (GBDeviceVersion)_deviceVersion {
  147. NSString *systemInfoString = [self _rawSystemInfoString];
  148. NSUInteger positionOfFirstInteger = [systemInfoString rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location;
  149. NSUInteger positionOfComma = [systemInfoString rangeOfString:@","].location;
  150. NSUInteger major = 0;
  151. NSUInteger minor = 0;
  152. if (positionOfComma != NSNotFound) {
  153. major = [[systemInfoString substringWithRange:NSMakeRange(positionOfFirstInteger, positionOfComma - positionOfFirstInteger)] integerValue];
  154. minor = [[systemInfoString substringFromIndex:positionOfComma + 1] integerValue];
  155. }
  156. return GBDeviceVersionMake(major, minor);
  157. }
  158. + (NSString *)_rawSystemInfoString {
  159. return [GBDeviceInfo_Common _sysctlStringForKey:kHardwareModelKey];
  160. }
  161. + (NSString *)_nodeName {
  162. return [NSString stringWithCString:[self _unameStruct].nodename encoding:NSUTF8StringEncoding];
  163. }
  164. + (BOOL)_isMacAppStoreAvailable {
  165. GBOSVersion osVersion = [self _osVersion];
  166. return ((osVersion.minor >= 7) ||
  167. (osVersion.minor == 6 && osVersion.patch >= 6));
  168. }
  169. + (BOOL)_isIAPAvailable {
  170. GBOSVersion osVersion = [self _osVersion];
  171. return (osVersion.minor >= 7);
  172. }
  173. @end