Sin descripción

UIDevice+YLDevice.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // UIDevice+YLDevice.m
  3. // YoungCatogory
  4. //
  5. // Created by kuxuan on 2017/7/17.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "UIDevice+YLDevice.h"
  9. #import "NSString+YLString.h"
  10. #import "NSData+YLData.h"
  11. #include <sys/types.h>
  12. #include <sys/sysctl.h>
  13. #include <sys/socket.h>
  14. #include <net/if.h>
  15. #include <net/if_dl.h>
  16. #import "sys/mount.h"
  17. static NSString * const YLUniqueIdentifierDefaultsKey = @"YLUniqueIdentifier";
  18. static NSString * const YLUserUniqueIdentifierDefaultsKey = @"YLUserUniqueIdentifier";
  19. @implementation UIDevice (YLDevice)
  20. + (NSString * _Nonnull)devicePlatform {
  21. size_t size;
  22. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  23. char *machine = malloc(size);
  24. sysctlbyname("hw.machine", machine, &size, NULL, 0);
  25. NSString *platform = [NSString stringWithUTF8String:machine];
  26. free(machine);
  27. return platform;
  28. }
  29. + (NSString * _Nonnull)devicePlatformString {
  30. NSString *platform = [self devicePlatform];
  31. // iPhone
  32. if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
  33. if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
  34. if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
  35. if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (GSM)";
  36. if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (Rev. A)";
  37. if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (CDMA)";
  38. if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
  39. if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)";
  40. if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (CDMA)";
  41. if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)";
  42. if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (Global)";
  43. if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)";
  44. if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (Global)";
  45. if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
  46. if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
  47. if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
  48. if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
  49. if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
  50. if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
  51. if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
  52. if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
  53. if ([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
  54. if ([platform isEqualToString:@"iPhone10,5"]) {
  55. return @"iPhone 8 plus";
  56. }
  57. if ([platform isEqualToString:@"iPhone10,2"]) {
  58. return @"iPhone 8 plus";
  59. }
  60. if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
  61. // iPod
  62. if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
  63. if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
  64. if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
  65. if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
  66. if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
  67. if ([platform isEqualToString:@"iPod7,1"]) return @"iPod Touch 6G";
  68. // iPad
  69. if ([platform isEqualToString:@"iPad1,1"]) return @"iPad 1";
  70. if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
  71. if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
  72. if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
  73. if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (32nm)";
  74. if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)";
  75. if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (CDMA)";
  76. if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (GSM)";
  77. if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)";
  78. if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)";
  79. if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (CDMA)";
  80. if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)";
  81. if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)";
  82. if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air (China)";
  83. if ([platform isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (WiFi)";
  84. if ([platform isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Cellular)";
  85. // iPad mini
  86. if ([platform isEqualToString:@"iPad2,5"]) return @"iPad mini (WiFi)";
  87. if ([platform isEqualToString:@"iPad2,6"]) return @"iPad mini (GSM)";
  88. if ([platform isEqualToString:@"iPad2,7"]) return @"iPad mini (CDMA)";
  89. if ([platform isEqualToString:@"iPad4,4"]) return @"iPad mini 2 (WiFi)";
  90. if ([platform isEqualToString:@"iPad4,5"]) return @"iPad mini 2 (Cellular)";
  91. if ([platform isEqualToString:@"iPad4,6"]) return @"iPad mini 2 (China)";
  92. if ([platform isEqualToString:@"iPad4,7"]) return @"iPad mini 3 (WiFi)";
  93. if ([platform isEqualToString:@"iPad4,8"]) return @"iPad mini 3 (Cellular)";
  94. if ([platform isEqualToString:@"iPad4,9"]) return @"iPad mini 3 (China)";
  95. if ([platform isEqualToString:@"iPad5,1"]) return @"iPad mini 4 (WiFi)";
  96. if ([platform isEqualToString:@"iPad5,2"]) return @"iPad mini 4 (Cellular)";
  97. // iPad Pro 9.7
  98. if ([platform isEqualToString:@"iPad6,3"]) return @"iPad Pro 9.7 (WiFi)";
  99. if ([platform isEqualToString:@"iPad6,4"]) return @"iPad Pro 9.7 (Cellular)";
  100. // iPad Pro 12.9
  101. if ([platform isEqualToString:@"iPad6,7"]) return @"iPad Pro 12.9 (WiFi)";
  102. if ([platform isEqualToString:@"iPad6,8"]) return @"iPad Pro 12.9 (Cellular)";
  103. // Apple TV
  104. if ([platform isEqualToString:@"AppleTV2,1"]) return @"Apple TV 2G";
  105. if ([platform isEqualToString:@"AppleTV3,1"]) return @"Apple TV 3G";
  106. if ([platform isEqualToString:@"AppleTV3,2"]) return @"Apple TV 3G";
  107. if ([platform isEqualToString:@"AppleTV5,3"]) return @"Apple TV 4G";
  108. // Apple Watch
  109. if ([platform isEqualToString:@"Watch1,1"]) return @"Apple Watch 38mm";
  110. if ([platform isEqualToString:@"Watch1,2"]) return @"Apple Watch 42mm";
  111. // Simulator
  112. if ([platform isEqualToString:@"i386"]) return @"Simulator";
  113. if ([platform isEqualToString:@"x86_64"]) return @"Simulator";
  114. return platform;
  115. }
  116. + (NSString * _Nonnull)currentResolution
  117. {
  118. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  119. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
  120. CGSize screenSize = [UIScreen mainScreen].bounds.size;
  121. NSInteger scale = [UIScreen mainScreen].scale;
  122. screenSize = CGSizeMake(screenSize.width * scale, screenSize.height * scale);
  123. if (screenSize.height <= 480.f) {
  124. return @"320x480";
  125. }else if(screenSize.height <= 960.f){
  126. return @"640x960";
  127. }else if (screenSize.height <= 1136.f){
  128. return @"640x1136";
  129. }else if (screenSize.height <= 1334.f){
  130. return @"750x1334";
  131. }else{
  132. return @"1242x2208";
  133. }
  134. }
  135. }
  136. return @"";
  137. }
  138. + (BOOL)isiPad {
  139. if ([[[self devicePlatform] substringToIndex:4] isEqualToString:@"iPad"]) {
  140. return YES;
  141. } else {
  142. return NO;
  143. }
  144. }
  145. + (BOOL)isiPhone {
  146. if ([[[self devicePlatform] substringToIndex:6] isEqualToString:@"iPhone"]) {
  147. return YES;
  148. } else {
  149. return NO;
  150. }
  151. }
  152. + (BOOL)isiPod {
  153. if ([[[self devicePlatform] substringToIndex:4] isEqualToString:@"iPod"]) {
  154. return YES;
  155. } else {
  156. return NO;
  157. }
  158. }
  159. + (BOOL)isAppleTV {
  160. if ([[[self devicePlatform] substringToIndex:7] isEqualToString:@"AppleTV"]) {
  161. return YES;
  162. } else {
  163. return NO;
  164. }
  165. }
  166. + (BOOL)isAppleWatch {
  167. if ([[[self devicePlatform] substringToIndex:5] isEqualToString:@"Watch"]) {
  168. return YES;
  169. } else {
  170. return NO;
  171. }
  172. }
  173. + (BOOL)isSimulator {
  174. if ([[self devicePlatform] isEqualToString:@"i386"] || [[self devicePlatform] isEqualToString:@"x86_64"]) {
  175. return YES;
  176. } else {
  177. return NO;
  178. }
  179. }
  180. + (NSInteger)iOSVersion {
  181. return [[[UIDevice currentDevice] systemVersion] integerValue];
  182. }
  183. + (NSUInteger)getSysInfo:(uint)typeSpecifier {
  184. size_t size = sizeof(int);
  185. int results;
  186. int mib[2] = {CTL_HW, typeSpecifier};
  187. sysctl(mib, 2, &results, &size, NULL, 0);
  188. return (NSUInteger) results;
  189. }
  190. + (NSUInteger)cpuFrequency {
  191. return [self getSysInfo:HW_CPU_FREQ];
  192. }
  193. + (NSUInteger)busFrequency {
  194. return [self getSysInfo:HW_TB_FREQ];
  195. }
  196. + (NSUInteger)ramSize {
  197. return [self getSysInfo:HW_MEMSIZE];
  198. }
  199. + (NSUInteger)cpuNumber {
  200. return [self getSysInfo:HW_NCPU];
  201. }
  202. + (NSUInteger)totalMemory {
  203. return [self getSysInfo:HW_PHYSMEM];
  204. }
  205. + (NSUInteger)userMemory {
  206. return [self getSysInfo:HW_USERMEM];
  207. }
  208. //手机总磁盘
  209. + (NSNumber * _Nonnull)totalDiskSpace {
  210. NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
  211. return [attributes objectForKey:NSFileSystemSize];
  212. }
  213. + (NSNumber * _Nonnull)freeDiskSpace {
  214. NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
  215. return [attributes objectForKey:NSFileSystemFreeSize];
  216. }
  217. + (NSString * _Nonnull)uniqueIdentifier {
  218. NSString *UUID;
  219. if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
  220. UUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
  221. } else {
  222. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  223. UUID = [defaults stringForKey:YLUniqueIdentifierDefaultsKey];
  224. if (!UUID) {
  225. UUID = [NSString generateUUID];
  226. [defaults setObject:UUID forKey:YLUniqueIdentifierDefaultsKey];
  227. [defaults synchronize];
  228. }
  229. }
  230. return UUID;
  231. }
  232. + (NSString * _Nonnull)idfaString{
  233. return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  234. }
  235. + (void)updateUniqueIdentifier:(NSObject * _Nonnull)uniqueIdentifier block:(void (^ _Nullable)(BOOL isValid, BOOL hasToUpdateUniqueIdentifier, NSString * _Nullable oldUUID))block {
  236. NSString *userUUID, *savedUUID = nil;
  237. BOOL isValid = false, hasToUpdate = false;
  238. if ([uniqueIdentifier isKindOfClass:[NSData class]]) {
  239. userUUID = [(NSData *)uniqueIdentifier convertUUIDToString];
  240. } else if ([uniqueIdentifier isKindOfClass:[NSString class]]) {
  241. userUUID = [(NSString *)uniqueIdentifier convertToAPNSUUID];
  242. }
  243. isValid = [userUUID isUUIDForAPNS];
  244. if (isValid) {
  245. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  246. savedUUID = [defaults stringForKey:YLUserUniqueIdentifierDefaultsKey];
  247. if (!savedUUID || ![savedUUID isEqualToString:userUUID]) {
  248. [defaults setObject:userUUID forKey:YLUserUniqueIdentifierDefaultsKey];
  249. [defaults synchronize];
  250. hasToUpdate = true;
  251. }
  252. }
  253. block(isValid, hasToUpdate, savedUUID);
  254. }
  255. //容量转换
  256. +(NSString *)fileSizeToString:(NSNumber *)fileSizeNumber
  257. {
  258. NSInteger fileSize = [fileSizeNumber integerValue];
  259. NSInteger KB = 1000;
  260. NSInteger MB = KB*KB;
  261. NSInteger GB = MB*KB;
  262. if (fileSize < 10) {
  263. return @"0 B";
  264. }else if (fileSize < KB) {
  265. return @"< 1 KB";
  266. }else if (fileSize < MB) {
  267. return [NSString stringWithFormat:@"%.1f KB",((CGFloat)fileSize)/KB];
  268. }else if (fileSize < GB) {
  269. return [NSString stringWithFormat:@"%.1f MB",((CGFloat)fileSize)/MB];
  270. }else {
  271. return [NSString stringWithFormat:@"%.1f GB",((CGFloat)fileSize)/GB];
  272. }
  273. }
  274. @end