简体   繁体   English

UIDevice currentDevice模型可能的值

[英]UIDevice currentDevice model possible values

What are all the possible values returned by [[UIDevice currentDevice] model]; [[UIDevice currentDevice] model];返回的所有可能值是什么? ? It isn't documented . 它没有记录

The possible vales are iPod touch , iPhone , iPhone Simulator , iPad , iPad Simulator 可能的价格包括iPod touchiPhoneiPhone SimulatoriPadiPad Simulator

If you want to know which hardware iOS is ruining on like iPhone3 , iPhone4 , iPhone5 etc below is the code for that 如果你想知道iOS正在破坏哪个硬件,如iPhone3iPhone4iPhone5等,下面就是代码


NOTE: The below code may not contain all device's string, I'm with other guys are maintaining the same code on GitHub so please take the latest code from there 注意:下面的代码可能不包含所有设备的字符串,我和其他人在GitHub上维护相同的代码,所以请从那里获取最新的代码

Objective-C : GitHub/DeviceUtil Objective-C: GitHub / DeviceUtil

Swift : GitHub/DeviceGuru Swift: GitHub / DeviceGuru


#include <sys/types.h>
#include <sys/sysctl.h>

- (NSString*)hardwareDescription {
    NSString *hardware = [self hardwareString];
    if ([hardware isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    if ([hardware isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    if ([hardware isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    if ([hardware isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    if ([hardware isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if ([hardware isEqualToString:@"iPod1,1"]) return @"iPodTouch 1G";
    if ([hardware isEqualToString:@"iPod2,1"]) return @"iPodTouch 2G";
    if ([hardware isEqualToString:@"iPad1,1"]) return @"iPad";
    if ([hardware isEqualToString:@"iPad2,6"]) return @"iPad Mini";
    if ([hardware isEqualToString:@"iPad4,1"]) return @"iPad Air WIFI";
    //there are lots of other strings too, checkout the github repo
    //link is given at the top of this answer

    if ([hardware isEqualToString:@"i386"]) return @"Simulator";
    if ([hardware isEqualToString:@"x86_64"]) return @"Simulator";

    return nil;
}

- (NSString*)hardwareString {
    size_t size = 100;
    char *hw_machine = malloc(size);
    int name[] = {CTL_HW,HW_MACHINE};
    sysctl(name, 2, hw_machine, &size, NULL, 0);
    NSString *hardware = [NSString stringWithUTF8String:hw_machine];
    free(hw_machine);
    return hardware;
}

I just did a test on iPod Touch, iPhone, Phone Retina, iPhone 5, iPad, iPad Retina and iPad Mini. 我刚刚对iPod Touch,iPhone,Phone Retina,iPhone 5,iPad,iPad Retina和iPad Mini进行了测试。 So this is my conclusion: 所以这是我的结论:

iPod touch
iPhone
iPad

On simulators - this could be useful if you're a developer working on features that sometimes do not work at all on simulators - you'll get these values: 在模拟器上 - 如果您是开发人员从事有时在模拟器上根本无法工作的功能,这可能很有用 - 您将获得以下值:

iPhone Simulator
iPad Simulator

I believe the best answer to explain(something which wasn't written here) Is to say that the value itself is a String value. 我相信解释的最佳答案(这里没有写的东西)是说值本身是一个字符串值。 and the possible answers are string eg: "iPhone","iPad" and etc.. 并且可能的答案是字符串,例如:“iPhone”,“iPad”等。

None of these answers are extendable for new model numbers. 对于新型号,这些答案都不可扩展。 Here is an enumeration: 这是一个枚举:

public enum DeviceType {
 case iPad(String?)
 case iPhone(String?)
 case simulator(String?)
 case appleTV(String?)
 case unknown
}

And Extension I wrote that I think is a little cleaner and a little more extendable for when new model number come out. 我写的扩展我认为当新的型号出现时,它会更清洁,更可扩展。

extension UIDevice {
    public static func getDevice() -> DeviceType {
        var info = utsname()
        uname(&info)
        let machineMirror = Mirror(reflecting: info.machine)
        let code = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else {
                return identifier
            }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }

        if code.lowercased().range(of: "ipad") != nil {
            if let range = code.lowercased().range(of: "ipad") {
                var mutate = code
                mutate.removeSubrange(range)
                return .iPad(mutate)
            }else{
                return .iPad(nil)
            }
        }else if code.lowercased().range(of: "iphone") != nil {
            if let range = code.lowercased().range(of: "iphone") {
                var mutate = code
                mutate.removeSubrange(range)
                return .iPhone(mutate)
            }else{
                return .iPhone(nil)
            }
        }else if code.lowercased().range(of: "i386") != nil || code.lowercased().range(of: "x86_64") != nil{
            return .simulator(code)
        }else if code.lowercased().range(of: "appletv") != nil {
            if let range = code.lowercased().range(of: "appletv") {
                var mutate = code
                mutate.removeSubrange(range)
                return .appleTV(mutate)
            }else{
                return .appleTV(nil)
            }
        }else{
            return .unknown
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [[UIDevice currentDevice] systemName] 的可能值; - Possible values for [[UIDevice currentDevice] systemName]; 替代[UIDevice currentDevice] .identifierForVendor - Alternative to [UIDevice currentDevice].identifierForVendor UIDevice.currentDevice()。beginGeneratingDeviceOrientationNotifications()无法正常工作 - UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications() not working [UIDevice currentDevice]可以使用什么? - What can be used with [UIDevice currentDevice]? Swift UIDevice.currentDevice()无法编译 - Swift UIDevice.currentDevice() not compiling 如何设置通知对象的“ UIDevice.currentDevice()”? - How to set the 'UIDevice.currentDevice()' for object of notification? 使用OCMock模拟[[UIDevice currentDevice] userInterfaceIdiom] - Using OCMock to mock [[UIDevice currentDevice] userInterfaceIdiom] iOS9:替代UIDevice.currentDevice()。setValue(...)强制定位 - iOS9: Alternative to UIDevice.currentDevice().setValue(…) to force orientation 不使用[[UIDevice currentDevice] setOrientation强制LandScape方向:UIInterfaceOrientationAnyOrientation] - Forcing LandScape orientation without using [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationAnyOrientation] 方法[[UIDevice currentDevice] uniqueIdentifier]不再允许,我需要一个替代方案 - Method [[UIDevice currentDevice] uniqueIdentifier] is not allowed any more, I need an alternative
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM