How to get device make and model on iOS?

后端 未结 19 962
爱一瞬间的悲伤
爱一瞬间的悲伤 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:26

    Below is the code for that (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)

    Objective-C : GitHub/DeviceUtil

    Swift : GitHub/DeviceGuru


    #include 
    #include 
    
    - (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;
    }
    

提交回复
热议问题