How to get device make and model on iOS?

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

    [[UIDevice currentDevice] model] just returns iPhone or iPod, you don't get the numbers of the model that would let you differentiate between different generations of devices.

    Code this method:

    #include 
    ...
    
    + (NSString *)getModel {
        size_t size;
        sysctlbyname("hw.machine", NULL, &size, NULL, 0);
        char *model = malloc(size);
        sysctlbyname("hw.machine", model, &size, NULL, 0);
        NSString *deviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
        free(model);
        return deviceModel;
    }
    

    And call the method [self getModel] whenever you need the model and you'll get i.e: "iPhone5,1" for the ridiculously thin and speedy iPhone 5.

    One good practice is to create a class called Utils.h/Utils.m and put methods like getModel there so you can get this info from anywhere in your App simply by importing the class and invoking [Utils getModel];

提交回复
热议问题