iOS开发------获取iPhone的具体型号

金毅
2023-12-01

开发过程中后台有这么一个需求,需要将iPhone的手机型号传过去,就写了一个UIDeviceCategroy来获取当前手机的具体型号,自然也包含了最新的SE,当然,这篇博文也会根据最新iPhone的发版不断地更新。没啥好说的,直接上代码吧,

声明文件如下:

//
//  UIDevice+YiPhoneModel.h
//  CityBike
//
//  Created by YueWen on 16/3/16.
//  Copyright © 2016年 YueWen. All rights reserved.
//

#import <Foundation/Foundation.h>

extern NSString * const Device_Simulator;
extern NSString * const Device_iPod1;
extern NSString * const Device_iPod2;
extern NSString * const Device_iPod3;
extern NSString * const Device_iPod4;
extern NSString * const Device_iPod5;
extern NSString * const Device_iPad2;
extern NSString * const Device_iPad3;
extern NSString * const Device_iPad4;
extern NSString * const Device_iPhone4;
extern NSString * const Device_iPhone4S;
extern NSString * const Device_iPhone5;
extern NSString * const Device_iPhone5S;
extern NSString * const Device_iPhone5C;
extern NSString * const Device_iPadMini1;
extern NSString * const Device_iPadMini2;
extern NSString * const Device_iPadMini3;
extern NSString * const Device_iPadAir1;
extern NSString * const Device_iPadAir2;
extern NSString * const Device_iPhone6;
extern NSString * const Device_iPhone6plus;
extern NSString * const Device_iPhone6S;
extern NSString * const Device_iPhone6Splus;
extern NSString * const Device_iPhoneSE;
extern NSString * const Device_iPhone7;
extern NSString * const Device_iPhone7plus;
extern NSString * const Device_Unrecognized;


/**
 *  获取手机型号的 类目
 */
@interface UIDevice (YiPhoneModel)

/**
 *  获取当前手机的具体型号
 *
 *  @return 手机型号字符串
 */
- (NSString *)phoneModel NS_AVAILABLE_IOS(7_0);

@end

实现方法如下:


#import "UIDevice+YiPhoneModel.h"
#import <sys/utsname.h>

NSString * const Device_Simulator = @"Simulator";
NSString * const Device_iPod1 = @"iPod1";
NSString * const Device_iPod2 = @"iPod2";
NSString * const Device_iPod3 = @"iPod3";
NSString * const Device_iPod4 = @"iPod4";
NSString * const Device_iPod5 = @"iPod5";
NSString * const Device_iPad2 = @"iPad2";
NSString * const Device_iPad3 = @"iPad3";
NSString * const Device_iPad4 = @"iPad4";
NSString * const Device_iPhone4 = @"iPhone 4";
NSString * const Device_iPhone4S = @"iPhone 4S";
NSString * const Device_iPhone5 = @"iPhone 5";
NSString * const Device_iPhone5S = @"iPhone 5S";
NSString * const Device_iPhone5C = @"iPhone 5C";
NSString * const Device_iPadMini1 = @"iPad Mini 1";
NSString * const Device_iPadMini2 = @"iPad Mini 2";
NSString * const Device_iPadMini3 = @"iPad Mini 3";
NSString * const Device_iPadAir1 = @"iPad Air 1";
NSString * const Device_iPadAir2 = @"iPad Mini 3";
NSString * const Device_iPhone6 = @"iPhone 6";
NSString * const Device_iPhone6plus = @"iPhone 6 Plus";
NSString * const Device_iPhone6S = @"iPhone 6S";
NSString * const Device_iPhone6Splus = @"iPhone 6S Plus";
NSString * const Device_iPhoneSE = @"iPhone SE";
NSString * const Device_iPhone7 = @"iPhone 7";
NSString * const Device_iPhone7plus = @"iPhone 7 Plus";
NSString * const Device_Unrecognized = @"unrecognized";


@implementation UIDevice (YiPhoneModel)


-(NSString *)phoneModel
{
    //创建utsname结构体
    struct utsname systemInfo;

    //构造结构体
    uname(&systemInfo);

    //将字符串进行转码
    NSString* code = [NSString stringWithCString:systemInfo.machine
                                        encoding:NSUTF8StringEncoding];

    static NSDictionary* deviceNamesByCode = nil;

    if (!deviceNamesByCode) {

        deviceNamesByCode = @{
                              @"i386"      : Device_Simulator,
                              @"x86_64"    : Device_Simulator,
                              @"iPod1,1"   : Device_iPod1,
                              @"iPod2,1"   : Device_iPod2,
                              @"iPod3,1"   : Device_iPod3,
                              @"iPod4,1"   : Device_iPod4,
                              @"iPod5,1"   : Device_iPod5,
                              @"iPad2,1"   : Device_iPad2,
                              @"iPad2,2"   : Device_iPad2,
                              @"iPad2,3"   : Device_iPad2,
                              @"iPad2,4"   : Device_iPad2,
                              @"iPad2,5"   : Device_iPadMini1,
                              @"iPad2,6"   : Device_iPadMini1,
                              @"iPad2,7"   : Device_iPadMini1,
                              @"iPhone3,1" : Device_iPhone4,
                              @"iPhone3,2" : Device_iPhone4,
                              @"iPhone3,3" : Device_iPhone4,
                              @"iPhone4,1" : Device_iPhone4S,
                              @"iPhone5,1" : Device_iPhone5,
                              @"iPhone5,2" : Device_iPhone5,
                              @"iPhone5,3" : Device_iPhone5C,
                              @"iPhone5,4" : Device_iPhone5C,
                              @"iPad3,1"   : Device_iPad3,
                              @"iPad3,2"   : Device_iPad3,
                              @"iPad3,3"   : Device_iPad3,
                              @"iPad3,4"   : Device_iPad4,
                              @"iPad3,5"   : Device_iPad4,
                              @"iPad3,6"   : Device_iPad4,
                              @"iPhone6,1" : Device_iPhone5S,
                              @"iPhone6,2" : Device_iPhone5S,
                              @"iPad4,1"   : Device_iPadAir1,
                              @"iPad4,2"   : Device_iPadAir2,
                              @"iPad4,4"   : Device_iPadMini2,
                              @"iPad4,5"   : Device_iPadMini2,
                              @"iPad4,6"   : Device_iPadMini2,
                              @"iPad4,7"   : Device_iPadMini3,
                              @"iPad4,8"   : Device_iPadMini3,
                              @"iPad4,9"   : Device_iPadMini3,
                              @"iPhone7,1" : Device_iPhone6plus,
                              @"iPhone7,2" : Device_iPhone6,
                              @"iPhone8,1" : Device_iPhone6S,
                              @"iPhone8,2" : Device_iPhone6Splus,
                              @"iPhone8,4" : Device_iPhoneSE,
                              @"iPhone9,1" : Device_iPhone7,                                   
                              @"iPhone9,2" : Device_iPhone7plus
                              };
    }

    NSString* deviceName = [deviceNamesByCode objectForKey:code];

    if(deviceName){
        return deviceName;
    }

    return Device_Unrecognized;
}

@end

使用以下语句来获取当前手机的具体型号:

NSString * phoneCode = [UIDevice currentDevice].phoneModel;
 类似资料: