How to get device make and model on iOS?

后端 未结 19 960
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 04:58

I was wondering if it\'s possible to determine what kind of iPhone (for example) the currentdevice is? I know it\'s possible to get the model through
NSString

19条回答
  •  终归单人心
    2020-11-22 05:05

    I've made the plist file to help you to get the complete name for each device (source code right at the end of my answer)

    Based on OhhMee's answer, you can use it like this:

    Swift 4.0

    static func deviceName() -> String {
    
            var systemInfo = utsname()
            uname(&systemInfo)
    
            guard let iOSDeviceModelsPath = Bundle.main.path(forResource: "iOSDeviceModelMapping", ofType: "plist") else { return "" }
            guard let iOSDevices = NSDictionary(contentsOfFile: iOSDeviceModelsPath) else { return "" }
    
            let machineMirror = Mirror(reflecting: systemInfo.machine)
            let identifier = machineMirror.children.reduce("") { identifier, element in
                guard let value = element.value as? Int8, value != 0 else { return identifier }
                return identifier + String(UnicodeScalar(UInt8(value)))
            }
    
            return iOSDevices.value(forKey: identifier) as! String
        }
    

    Don't forget to add #import in your Bridging Header.

    Objective C

    #import 
    
    NSString *machineName()
    {
        struct utsname systemInfo;
        uname(&systemInfo);
    
        NSString *iOSDeviceModelsPath = [[NSBundle mainBundle] pathForResource:@"iOSDeviceModelMapping" ofType:@"plist"];
        NSDictionary *iOSDevices = [NSDictionary dictionaryWithContentsOfFile:iOSDeviceModelsPath];
    
        NSString* deviceModel = [NSString stringWithCString:systemInfo.machine
                                                   encoding:NSUTF8StringEncoding];
    
        return [iOSDevices valueForKey:deviceModel];
    }
    

    The plist file :

    
    
    
    
        x86_64
        Simulator
        i386
        Simulator
        iPod1,1
        iPod Touch 1st Gen
        iPod2,1
        iPod Touch 2nd Gen
        iPod3,1
        iPod Touch 3rd Gen
        iPod4,1
        iPod Touch 4th Gen
        iPod5,1
        iPod Touch 5th Gen
        iPod7,1
        iPod Touch 6th Gen
        iPhone1,1
        iPhone
        iPhone1,2
        iPhone 3G
        iPhone2,1
        iPhone 3GS
        iPhone3,1
        iPhone 4
        iPhone3,2
        iPhone 4
        iPhone3,3
        iPhone 4
        iPhone4,1
        iPhone 4S
        iPhone5,1
        iPhone 5 model A1428
        iPhone5,2
        iPhone 5 model A1429
        iPhone5,3
        iPhone 5C
        iPhone5,4
        iPhone 5C
        iPhone6,1
        iPhone 5S
        iPhone6,2
        iPhone 5S
        iPhone7,2
        iPhone 6
        iPhone7,1
        iPhone 6 Plus
        iPhone8,1
        iPhone 6S
        iPhone8,2
        iPhone 6S Plus
        iPhone8,4
        iPhone SE
        iPhone9,1
        iPhone 7
        iPhone9,2
        iPhone 7 Plus
        iPhone9,3
        iPhone 7
        iPhone9,4
        iPhone 7 Plus
        iPhone10,1
        iPhone 8
        iPhone10,4
        iPhone 8
        iPhone10,2
        iPhone 8 Plus
        iPhone10,5
        iPhone 8 Plus
        iPhone10,3
        iPhone X
        iPhone10,6
        iPhone X
        iPhone11,2
        iPhone XS
        iPhone11,4
        iPhone XS Max
        iPhone11,6
        iPhone XS Max
        iPhone11,8
        iPhone XR
        iPad1,1
        iPad
        iPad2,1
        iPad 2
        iPad2,2
        iPad 2
        iPad2,3
        iPad 2
        iPad2,4
        iPad 2
        iPad3,1
        iPad 3rd Gen
        iPad3,2
        iPad 3rd Gen
        iPad3,3
        iPad 3rd Gen
        iPad3,4
        iPad 4th Gen
        iPad3,5
        iPad 4th Gen
        iPad3,6
        iPad 4th Gen
        iPad4,1
        iPad Air
        iPad4,2
        iPad Air
        iPad4,3
        iPad Air
        iPad2,5
        iPad Mini 1st Gen
        iPad2,6
        iPad Mini 1st Gen
        iPad2,7
        iPad Mini 1st Gen
        iPad4,4
        iPad Mini 2nd Gen
        iPad4,5
        iPad Mini 2nd Gen
        iPad4,6
        iPad Mini 2nd Gen
        iPad4,7
        iPad Mini 3rd Gen
        iPad4,8
        iPad Mini 3rd Gen
        iPad4,9
        iPad Mini 3rd Gen
        iPad5,1
        iPad Mini 4
        iPad5,2
        iPad Mini 4
        iPad5,3
        iPad Air 2
        iPad5,4
        iPad Air 2
        iPad6,3
        iPad Pro 9.7 inch
        iPad6,4
        iPad Pro 9.7 inch
        iPad6,7
        iPad Pro 12.9 inch
        iPad6,8
        iPad Pro 12.9 inch
        iPad7,1
        iPad Pro 12.9 inch 2nd Gen
        iPad7,2
        iPad Pro 12.9 inch 2nd Gen
        iPad7,3
        iPad Pro 10.5 inch
        iPad7,4
        iPad Pro 10.5 inch
        iPad8,1
        iPad Pro 11 inch
        iPad8,2
        iPad Pro 11 inch
        iPad8,3
        iPad Pro 11 inch
        iPad8,4
        iPad Pro 11 inch
        iPad8,5
        iPad Pro 12.9 inch 3rd Gen
        iPad8,6
        iPad Pro 12.9 inch 3rd Gen
        iPad8,7
        iPad Pro 12.9 inch 3rd Gen
        iPad8,8
        iPad Pro 12.9 inch 3rd Gen
    
    
    

提交回复
热议问题