123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- //
- // UIDevice+YLDevice.m
- // YoungCatogory
- //
- // Created by kuxuan on 2017/7/17.
- // Copyright © 2017年 kuxuan. All rights reserved.
- //
- #import "UIDevice+YLDevice.h"
- #import "NSString+YLString.h"
- #import "NSData+YLData.h"
- #include <sys/types.h>
- #include <sys/sysctl.h>
- #include <sys/socket.h>
- #include <net/if.h>
- #include <net/if_dl.h>
- #import "sys/mount.h"
- static NSString * const YLUniqueIdentifierDefaultsKey = @"YLUniqueIdentifier";
- static NSString * const YLUserUniqueIdentifierDefaultsKey = @"YLUserUniqueIdentifier";
- @implementation UIDevice (YLDevice)
- + (NSString * _Nonnull)devicePlatform {
- size_t size;
- sysctlbyname("hw.machine", NULL, &size, NULL, 0);
- char *machine = malloc(size);
- sysctlbyname("hw.machine", machine, &size, NULL, 0);
- NSString *platform = [NSString stringWithUTF8String:machine];
- free(machine);
- return platform;
- }
- + (NSString * _Nonnull)devicePlatformString {
- NSString *platform = [self devicePlatform];
- // iPhone
- if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
- if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
- if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
- if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (GSM)";
- if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (Rev. A)";
- if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (CDMA)";
- if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
- if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)";
- if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (CDMA)";
- if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)";
- if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (Global)";
- if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)";
- if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (Global)";
- if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
- if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
- if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
- if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
- if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
- if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
- if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
-
- if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
-
- if ([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
-
- if ([platform isEqualToString:@"iPhone10,5"]) {
- return @"iPhone 8 plus";
- }
-
- if ([platform isEqualToString:@"iPhone10,2"]) {
- return @"iPhone 8 plus";
- }
- if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
- // iPod
- if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
- if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
- if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
- if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
- if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
- if ([platform isEqualToString:@"iPod7,1"]) return @"iPod Touch 6G";
- // iPad
- if ([platform isEqualToString:@"iPad1,1"]) return @"iPad 1";
- if ([platform isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
- if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
- if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
- if ([platform isEqualToString:@"iPad2,4"]) return @"iPad 2 (32nm)";
- if ([platform isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)";
- if ([platform isEqualToString:@"iPad3,2"]) return @"iPad 3 (CDMA)";
- if ([platform isEqualToString:@"iPad3,3"]) return @"iPad 3 (GSM)";
- if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)";
- if ([platform isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)";
- if ([platform isEqualToString:@"iPad3,6"]) return @"iPad 4 (CDMA)";
- if ([platform isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)";
- if ([platform isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)";
- if ([platform isEqualToString:@"iPad4,3"]) return @"iPad Air (China)";
- if ([platform isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (WiFi)";
- if ([platform isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Cellular)";
- // iPad mini
- if ([platform isEqualToString:@"iPad2,5"]) return @"iPad mini (WiFi)";
- if ([platform isEqualToString:@"iPad2,6"]) return @"iPad mini (GSM)";
- if ([platform isEqualToString:@"iPad2,7"]) return @"iPad mini (CDMA)";
- if ([platform isEqualToString:@"iPad4,4"]) return @"iPad mini 2 (WiFi)";
- if ([platform isEqualToString:@"iPad4,5"]) return @"iPad mini 2 (Cellular)";
- if ([platform isEqualToString:@"iPad4,6"]) return @"iPad mini 2 (China)";
- if ([platform isEqualToString:@"iPad4,7"]) return @"iPad mini 3 (WiFi)";
- if ([platform isEqualToString:@"iPad4,8"]) return @"iPad mini 3 (Cellular)";
- if ([platform isEqualToString:@"iPad4,9"]) return @"iPad mini 3 (China)";
- if ([platform isEqualToString:@"iPad5,1"]) return @"iPad mini 4 (WiFi)";
- if ([platform isEqualToString:@"iPad5,2"]) return @"iPad mini 4 (Cellular)";
- // iPad Pro 9.7
- if ([platform isEqualToString:@"iPad6,3"]) return @"iPad Pro 9.7 (WiFi)";
- if ([platform isEqualToString:@"iPad6,4"]) return @"iPad Pro 9.7 (Cellular)";
- // iPad Pro 12.9
- if ([platform isEqualToString:@"iPad6,7"]) return @"iPad Pro 12.9 (WiFi)";
- if ([platform isEqualToString:@"iPad6,8"]) return @"iPad Pro 12.9 (Cellular)";
- // Apple TV
- if ([platform isEqualToString:@"AppleTV2,1"]) return @"Apple TV 2G";
- if ([platform isEqualToString:@"AppleTV3,1"]) return @"Apple TV 3G";
- if ([platform isEqualToString:@"AppleTV3,2"]) return @"Apple TV 3G";
- if ([platform isEqualToString:@"AppleTV5,3"]) return @"Apple TV 4G";
- // Apple Watch
- if ([platform isEqualToString:@"Watch1,1"]) return @"Apple Watch 38mm";
- if ([platform isEqualToString:@"Watch1,2"]) return @"Apple Watch 42mm";
- // Simulator
- if ([platform isEqualToString:@"i386"]) return @"Simulator";
- if ([platform isEqualToString:@"x86_64"]) return @"Simulator";
-
- return platform;
- }
- + (NSString * _Nonnull)currentResolution
- {
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
- if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
- CGSize screenSize = [UIScreen mainScreen].bounds.size;
- NSInteger scale = [UIScreen mainScreen].scale;
- screenSize = CGSizeMake(screenSize.width * scale, screenSize.height * scale);
- if (screenSize.height <= 480.f) {
- return @"320x480";
- }else if(screenSize.height <= 960.f){
- return @"640x960";
- }else if (screenSize.height <= 1136.f){
- return @"640x1136";
- }else if (screenSize.height <= 1334.f){
- return @"750x1334";
- }else{
- return @"1242x2208";
- }
- }
- }
- return @"";
- }
- + (BOOL)isiPad {
- if ([[[self devicePlatform] substringToIndex:4] isEqualToString:@"iPad"]) {
- return YES;
- } else {
- return NO;
- }
- }
- + (BOOL)isiPhone {
- if ([[[self devicePlatform] substringToIndex:6] isEqualToString:@"iPhone"]) {
- return YES;
- } else {
- return NO;
- }
- }
- + (BOOL)isiPod {
- if ([[[self devicePlatform] substringToIndex:4] isEqualToString:@"iPod"]) {
- return YES;
- } else {
- return NO;
- }
- }
- + (BOOL)isAppleTV {
- if ([[[self devicePlatform] substringToIndex:7] isEqualToString:@"AppleTV"]) {
- return YES;
- } else {
- return NO;
- }
- }
- + (BOOL)isAppleWatch {
- if ([[[self devicePlatform] substringToIndex:5] isEqualToString:@"Watch"]) {
- return YES;
- } else {
- return NO;
- }
- }
- + (BOOL)isSimulator {
- if ([[self devicePlatform] isEqualToString:@"i386"] || [[self devicePlatform] isEqualToString:@"x86_64"]) {
- return YES;
- } else {
- return NO;
- }
- }
- + (NSInteger)iOSVersion {
- return [[[UIDevice currentDevice] systemVersion] integerValue];
- }
- + (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_TB_FREQ];
- }
- + (NSUInteger)ramSize {
- return [self getSysInfo:HW_MEMSIZE];
- }
- + (NSUInteger)cpuNumber {
- return [self getSysInfo:HW_NCPU];
- }
- + (NSUInteger)totalMemory {
- return [self getSysInfo:HW_PHYSMEM];
- }
- + (NSUInteger)userMemory {
- return [self getSysInfo:HW_USERMEM];
- }
- //手机总磁盘
- + (NSNumber * _Nonnull)totalDiskSpace {
- NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
- return [attributes objectForKey:NSFileSystemSize];
- }
- + (NSNumber * _Nonnull)freeDiskSpace {
- NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
- return [attributes objectForKey:NSFileSystemFreeSize];
- }
- + (NSString * _Nonnull)uniqueIdentifier {
- NSString *UUID;
- if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
- UUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
- } else {
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- UUID = [defaults stringForKey:YLUniqueIdentifierDefaultsKey];
- if (!UUID) {
- UUID = [NSString generateUUID];
- [defaults setObject:UUID forKey:YLUniqueIdentifierDefaultsKey];
- [defaults synchronize];
- }
- }
- return UUID;
- }
- + (NSString * _Nonnull)idfaString{
- return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
- }
- + (void)updateUniqueIdentifier:(NSObject * _Nonnull)uniqueIdentifier block:(void (^ _Nullable)(BOOL isValid, BOOL hasToUpdateUniqueIdentifier, NSString * _Nullable oldUUID))block {
- NSString *userUUID, *savedUUID = nil;
- BOOL isValid = false, hasToUpdate = false;
-
- if ([uniqueIdentifier isKindOfClass:[NSData class]]) {
- userUUID = [(NSData *)uniqueIdentifier convertUUIDToString];
- } else if ([uniqueIdentifier isKindOfClass:[NSString class]]) {
- userUUID = [(NSString *)uniqueIdentifier convertToAPNSUUID];
- }
-
- isValid = [userUUID isUUIDForAPNS];
-
- if (isValid) {
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- savedUUID = [defaults stringForKey:YLUserUniqueIdentifierDefaultsKey];
- if (!savedUUID || ![savedUUID isEqualToString:userUUID]) {
- [defaults setObject:userUUID forKey:YLUserUniqueIdentifierDefaultsKey];
- [defaults synchronize];
-
- hasToUpdate = true;
- }
- }
-
- block(isValid, hasToUpdate, savedUUID);
- }
- //容量转换
- +(NSString *)fileSizeToString:(NSNumber *)fileSizeNumber
- {
- NSInteger fileSize = [fileSizeNumber integerValue];
-
-
- NSInteger KB = 1000;
- NSInteger MB = KB*KB;
- NSInteger GB = MB*KB;
-
- if (fileSize < 10) {
- return @"0 B";
- }else if (fileSize < KB) {
- return @"< 1 KB";
- }else if (fileSize < MB) {
- return [NSString stringWithFormat:@"%.1f KB",((CGFloat)fileSize)/KB];
- }else if (fileSize < GB) {
- return [NSString stringWithFormat:@"%.1f MB",((CGFloat)fileSize)/MB];
- }else {
- return [NSString stringWithFormat:@"%.1f GB",((CGFloat)fileSize)/GB];
- }
- }
- @end
|