如何检测iOS模拟器正在模拟的具体设备型号?
获取iOS模拟器的具体设备型号
原生代码实现(Swift)
通过读取模拟器独有的环境变量SIMULATOR_MODEL_IDENTIFIER拿到硬件型号标识,再映射为用户友好的设备名称:
import UIKit func getSimulatorDeviceName() -> String? { guard let modelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] else { return nil } // 可根据苹果新机型更新映射表 let modelMap: [String: String] = [ "iPhone16,2": "iPhone 15 Pro Max", "iPhone16,1": "iPhone 15 Pro", "iPhone15,3": "iPhone 15 Plus", "iPhone15,2": "iPhone 15", "iPhone14,8": "iPhone SE (3rd generation)", "iPhone14,3": "iPhone 13 Pro Max", "iPhone14,2": "iPhone 13 Pro" ] return modelMap[modelIdentifier] ?? modelIdentifier } // 调用示例 if let deviceName = getSimulatorDeviceName() { print("正在模拟:\(deviceName)") }
原生代码实现(Objective-C)
逻辑和Swift一致,通过环境变量获取型号标识后映射:
#import <Foundation/Foundation.h> NSString *getSimulatorDeviceName() { NSString *modelIdentifier = [[NSProcessInfo processInfo] environment][@"SIMULATOR_MODEL_IDENTIFIER"]; if (!modelIdentifier) { return nil; } NSDictionary *modelMap = @{ @"iPhone16,2": @"iPhone 15 Pro Max", @"iPhone16,1": @"iPhone 15 Pro", @"iPhone15,3": @"iPhone 15 Plus", @"iPhone15,2": @"iPhone 15", @"iPhone14,8": @"iPhone SE (3rd generation)" }; return modelMap[modelIdentifier] ?: modelIdentifier; } // 调用示例 NSString *deviceName = getSimulatorDeviceName(); if (deviceName) { NSLog(@"正在模拟:%@", deviceName); }
命令行方式(Mac端调试用)
如果需要在终端或脚本中获取模拟器信息,使用xcrun simctl工具:
查看所有已启动的模拟器:
xcrun simctl list devices | grep Booted获取指定模拟器的型号标识:
xcrun simctl getenv <模拟器UDID> SIMULATOR_MODEL_IDENTIFIER
替换<模拟器UDID>为已启动模拟器的UDID(可通过上面的命令获取)。
内容的提问来源于stack exchange,提问作者DefenestrationDay
相关产品推荐
相关产品推荐

