Skip to content

Commit dd2633a

Browse files
[HEAP-44383] - Improved iOS device model data (#110)
1 parent b50474d commit dd2633a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Changed process to retrieve iOS device model. 'sysctlbyname' is now used to retrieve the
13+
detailed hardware identifier (e.g., "iPhone10,3"). This results in more specific
14+
model identification than the generic `UIDevice.model` approach which is now used
15+
as a fallback.
16+
1017
## [0.2.1]
1118

1219
### Fixed

Development/Sources/HeapSwiftCore/Protobufs/Extensions/DeviceInfo.iOS.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@ import UIKit
44
import CoreTelephony
55

66
extension DeviceInfo {
7+
8+
/// Returns the specific model name of the current device.
9+
///
10+
/// `UIDevice.model` only provides a generic model name (e.g., "iPhone"). This method, on the other hand,
11+
/// utilizes `sysctlbyname` to retrieve the detailed hardware identifier (e.g., "iPhone10,3" for iPhone X).
12+
/// If `sysctlbyname` fails, the function returns `nil`.
13+
var detailedModelName: String? {
14+
var size: Int = 0
15+
guard sysctlbyname("hw.machine", nil, &size, nil, 0) == 0 else { return nil }
16+
var machine = [CChar](repeating: 0, count: size)
17+
guard sysctlbyname("hw.machine", &machine, &size, nil, 0) == 0 else { return nil }
18+
return String(cString: machine)
19+
}
20+
721
static func current(with settings: FieldSettings, includeCarrier: Bool) -> DeviceInfo {
822
let device = UIDevice.current
923
var deviceInfo = DeviceInfo()
1024
deviceInfo.type = getDeviceType(device)
1125
deviceInfo.platform = getPlatform(device)
12-
deviceInfo.model = getMacModel() ?? device.model
26+
deviceInfo.model = getMacModel() ?? deviceInfo.detailedModelName ?? device.model
1327
if includeCarrier,
1428
let carrier = getCarrier() {
1529
deviceInfo.carrier = carrier

0 commit comments

Comments
 (0)