Determine device (iPhone, iPod Touch) with iOS

后端 未结 30 1779
礼貌的吻别
礼貌的吻别 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:04
    NSString *deviceType = [UIDevice currentDevice].model;
    
    0 讨论(0)
  • 2020-11-21 12:04

    For simple comparison I always like macro:

    #define IS_IPOD [[UIDevice currentDevice].model containsString:@"iPod"]
    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    

    It's simple and easy to use.

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

    You can check GBDeviceInfo on GitHub, also available via CocoaPods. It provides simple API for detecting various properties with support of all latest devices:

    • Device family

    [GBDeviceInfo deviceDetails].family == GBDeviceFamilyiPhone;

    • Device model

    [GBDeviceInfo deviceDetails].model == GBDeviceModeliPhone6.

    For more see Readme.

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

    I took it a bit further and converted the big "isEqualToString" block into a classification of bit masks for the device type, the generation, and that other qualifier after the comma (I'm calling it the sub generation). It is wrapped in a class with a singleton call SGPlatform which avoids a lot of repetitive string operations. Code is available https://github.com/danloughney/spookyGroup

    The class lets you do things like this:

    if ([SGPlatform iPad] && [SGPlatform generation] > 3) {
        // set for high performance
    }
    

    and

    switch ([SGPlatform deviceMask]) {
    case DEVICE_IPHONE:
        break;
    case DEVICE_IPAD:
        break;
    case DEVICE_IPAD_MINI:
        break;
    }
    

    The classification of the devices is in the platformBits method. That method should be very familiar to the readers of this thread. I have classified the devices by their type and generation because I'm mostly interested in the overall performance, but the source can be tweaked to provide any classification that you are interested in, retina screen, networking capabilities, etc..

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

    To identifiy iPhone 4S, simply check the following:

    var isIphone4S: Bool {
    
        let width = UIScreen.main.bounds.size.width
        let height = UIScreen.main.bounds.size.height
        let proportions = width > height ? width / height : height / width
    
        return proportions == 1.5 && UIDevice.current.model == "iPhone"
    }
    
    0 讨论(0)
  • 2020-11-21 12:07

    The possible vales of

    [[UIDevice currentDevice] model];
    

    are iPod touch, iPhone, iPhone Simulator, iPad, iPad Simulator

    If you want to know which hardware iOS is ruining on like iPhone3, iPhone4, iPhone5 etc below is the code for that


    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

    Objective-C : GitHub/DeviceUtil

    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;
    }
    
    0 讨论(0)
提交回复
热议问题