Determine device (iPhone, iPod Touch) with iOS

后端 未结 30 1747
礼貌的吻别
礼貌的吻别 2020-11-21 11:29

Is there a way to determine the device running an application. I want to distinguish between iPhone and iPod Touch, if possible.

相关标签:
30条回答
  • 2020-11-21 12:07

    Answer in Swift 3,

    struct DeviceInformation {
        // UIDevice.current.model's value is "iPhone" or "iPad",which does not include details; the following "model" is detailed, such as, iPhone7,1 is actually iPhone 6 plus
        static let model: String = {
            var size = 0
            sysctlbyname("hw.machine", nil, &size, nil, 0)
            var machine = [CChar](repeating: 0,  count: Int(size))
            sysctlbyname("hw.machine", &machine, &size, nil, 0)
            return String(cString: machine)
        }()
    
        static let uuid = UIDevice.current.identifierForVendor?.uuidString ?? ""
        static let idForAdService = ASIdentifierManager.shared().advertisingIdentifier.uuidString
    
        static func diviceTypeReadableName() -> String {
            switch model {
            case "iPhone1,1":   return "iPhone 1G"
            case "iPhone1,2":   return "iPhone 3G"
            case "iPhone2,1":   return "iPhone 3GS"
            case "iPhone3,1":   return "iPhone 4"
            case "iPhone3,3":   return "iPhone 4 (Verizon)"
            case "iPhone4,1":   return "iPhone 4S"
            case "iPhone5,1":   return "iPhone 5 (GSM)"
            case "iPhone5,2":   return "iPhone 5 (GSM+CDMA)"
            case "iPhone5,3":   return "iPhone 5c (GSM)"
            case "iPhone5,4":   return "iPhone 5c (GSM+CDMA)"
            case "iPhone6,1":   return "iPhone 5s (GSM)"
            case "iPhone6,2":   return "iPhone 5s (GSM+CDMA)"
            case "iPhone7,2":   return "iPhone 6"
            case "iPhone7,1":   return "iPhone 6 Plus"
            case "iPhone8,1":   return "iPhone 6s"
            case "iPhone8,2":   return "iPhone 6s Plus"
            case "iPhone8,4":   return "iPhone SE"
            case "iPhone9,1":   return "iPhone 7"
            case "iPhone9,3":   return "iPhone 7"
            case "iPhone9,2":   return "iPhone 7 Plus"
            case "iPhone9,4":   return "iPhone 7 Plus"
            case "iPod1,1":     return "iPod Touch 1G"
            case "iPod2,1":     return "iPod Touch 2G"
            case "iPod3,1":     return "iPod Touch 3G"
            case "iPod4,1":     return "iPod Touch 4G"
            case "iPod5,1":     return "iPod Touch 5G"
            case "iPod7,1":     return "iPod Touch 6G"
            case "iPad1,1":     return "iPad 1G"
            case "iPad2,1":     return "iPad 2 (Wi-Fi)"
            case "iPad2,2":     return "iPad 2 (GSM)"
            case "iPad2,3":     return "iPad 2 (CDMA)"
            case "iPad2,4":     return "iPad 2 (Wi-Fi)"
            case "iPad2,5":     return "iPad Mini (Wi-Fi)"
            case "iPad2,6":     return "iPad Mini (GSM)"
            case "iPad2,7":     return "iPad Mini (GSM+CDMA)"
            case "iPad3,1":     return "iPad 3 (Wi-Fi)"
            case "iPad3,2":     return "iPad 3 (GSM+CDMA)"
            case "iPad3,3":     return "iPad 3 (GSM)"
            case "iPad3,4":     return "iPad 4 (Wi-Fi)"
            case "iPad3,5":     return "iPad 4 (GSM)"
            case "iPad3,6":     return "iPad 4 (GSM+CDMA)"
            case "iPad4,1":     return "iPad Air (Wi-Fi)"
            case "iPad4,2":     return "iPad Air (Cellular)"
            case "iPad4,3":     return "iPad Air (China)"
            case "iPad4,4":     return "iPad Mini 2G (Wi-Fi)"
            case "iPad4,5":     return "iPad Mini 2G (Cellular)"
            case "iPad4,6":     return "iPad Mini 2G (China)"
            case "iPad4,7":     return "iPad Mini 3 (Wi-Fi)"
            case "iPad4,8":     return "iPad Mini 3 (Cellular)"
            case "iPad4,9":     return "iPad Mini 3 (China)"
            case "iPad5,1":     return "iPad Mini 4 (Wi-Fi)"
            case "iPad5,2":     return "iPad Mini 4 (Cellular)"
            case "iPad5,3":     return "iPad Air 2 (Wi-Fi)"
            case "iPad5,4":     return "iPad Air 2 (Cellular)"
            case "iPad6,3":     return "iPad Pro 9.7' (Wi-Fi)"
            case "iPad6,4":     return "iPad Pro 9.7' (Cellular)"
            case "iPad6,7":     return "iPad Pro 12.9' (Wi-Fi)"
            case "iPad6,8":     return "iPad Pro 12.9' (Cellular)"
            case "AppleTV2,1":  return "Apple TV 2G"
            case "AppleTV3,1":  return "Apple TV 3"
            case "AppleTV3,2":  return "Apple TV 3 (2013)"
            case "AppleTV5,3":  return "Apple TV 4"
            case "Watch1,1":    return "Apple Watch Series 1 (38mm, S1)"
            case "Watch1,2":    return "Apple Watch Series 1 (42mm, S1)"
            case "Watch2,6":    return "Apple Watch Series 1 (38mm, S1P)"
            case "Watch2,7":    return "Apple Watch Series 1 (42mm, S1P)"
            case "Watch2,3":    return "Apple Watch Series 2 (38mm, S2)"
            case "Watch2,4":    return "Apple Watch Series 2 (42mm, S2)"
            case "i386":        return "Simulator"
            case "x86_64":      return "Simulator"
    
            default:
                return ""
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 12:09

    There's a pretty mature library (by me) for that called SDVersion. You can check for running device's model, screen size and many other parameters. It also supports OSX.

    Example:

          // Check for device model
          if ([SDVersion deviceVersion] == iPhone6)
               NSLog(@"You got the iPhone 6. Sweet                                                                     
    0 讨论(0)
  • 2020-11-21 12:12

    I like Erica Sadun's stuff. She includes AppleTV devices and others you might not think of.

    https://github.com/erica/uidevice-extension/blob/master/UIDevice-Hardware.h

    0 讨论(0)
  • 2020-11-21 12:13

    I'd like to add that to retrieve the front and enclosure color of the device there's a private API:

    UIDevice *device = [UIDevice currentDevice];
    SEL selector = NSSelectorFromString([device.systemVersion hasPrefix:@"7"] ? @"_deviceInfoForKey:" :  @"deviceInfoForKey:");
    if ([device respondsToSelector:selector]) {
        NSLog(@"DeviceColor: %@ DeviceEnclosureColor: %@", [device performSelector:selector withObject:@"DeviceColor"], [device performSelector:selector withObject:@"DeviceEnclosureColor"]);
    }
    

    I've blogged about this and provide a sample app:

    http://www.futuretap.com/blog/device-colors/

    0 讨论(0)
  • 2020-11-21 12:15

    Updated platform strings for iPad Air 2 and iPad mini 3:

    - (NSString *)platformString
    {
        NSString *platform = [self platform];
    
        if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
        if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
        if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
        if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
        if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
        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 (GSM+CDMA)";
        if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
        if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
        if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
        if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
        if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
        if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
        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:@"iPad1,1"])      return @"iPad";
        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 (WiFi)";
        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 (GSM+CDMA)";
        if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
        if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+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 (GSM+CDMA)";
        if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    
        if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
        if ([platform isEqualToString:@"iPad4,4"])      return @"iPad mini 2G (WiFi)";
        if ([platform isEqualToString:@"iPad4,5"])      return @"iPad mini 2G (Cellular)";
    
        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 Model)";
    
        if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
        if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
        if ([platform isEqualToString:@"iPad6,8"])      return @"iPad Pro";
    
        if ([platform isEqualToString:@"i386"])         return @"Simulator";
        if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    
        return platform;
    }
    
    0 讨论(0)
  • 2020-11-21 12:15

    Here's a minor update with new models:

    - (NSString *) platformString{
        NSString *platform = [self platform];
        if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
        if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
        if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
        if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
        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:@"i386"])   return @"iPhone Simulator";
        return platform;
    }
    
    0 讨论(0)
提交回复
热议问题