Skip to content

Instantly share code, notes, and snippets.

@viniciusfbb
Last active January 21, 2024 20:03
Show Gist options
  • Save viniciusfbb/259487e36e485aa709423e71baf54fe6 to your computer and use it in GitHub Desktop.
Save viniciusfbb/259487e36e485aa709423e71baf54fe6 to your computer and use it in GitHub Desktop.
Get the device model name in iOS in Delphi (Firemonkey) applications
unit DeviceModel.iOS;
interface
function GetDeviceModelName: string;
function GetDeviceModelPrettyName: string;
implementation
uses
System.SysUtils,
System.Generics.Collections,
System.Generics.Defaults,
Posix.SysSysctl,
Posix.StdDef;
function GetDeviceModelName: string;
var
LSize: size_t;
LDeviceModelBuffer: array of Byte;
begin
sysctlbyname('hw.machine', nil, @LSize, nil, 0);
if LSize > 0 then
begin
SetLength(LDeviceModelBuffer, LSize);
sysctlbyname('hw.machine', @LDeviceModelBuffer[0], @LSize, nil, 0);
Result := UTF8ToString(MarshaledAString(LDeviceModelBuffer));
end
else
Result := '';
end;
// Pretty name list based on https://gist.github.com/adamawolf/3048717
function GetDeviceModelPrettyName: string;
type
TAppleDevice = record
Machine: string;
Name: string;
end;
const
AppleDevices: array[0..173] of TAppleDevice = (
(Machine: 'arm64'; Name: 'iPhone Simulator'),
(Machine: 'i386'; Name: 'iPhone Simulator'),
(Machine: 'ipad1,1'; Name: 'iPad'),
(Machine: 'ipad1,2'; Name: 'iPad 3G'),
(Machine: 'ipad11,1'; Name: 'iPad mini 5th Gen (WiFi)'),
(Machine: 'ipad11,2'; Name: 'iPad mini 5th Gen'),
(Machine: 'ipad11,3'; Name: 'iPad Air 3rd Gen (WiFi)'),
(Machine: 'ipad11,4'; Name: 'iPad Air 3rd Gen'),
(Machine: 'ipad11,6'; Name: 'iPad 8th Gen (WiFi)'),
(Machine: 'ipad11,7'; Name: 'iPad 8th Gen (WiFi+Cellular)'),
(Machine: 'ipad12,1'; Name: 'iPad 9th Gen (WiFi)'),
(Machine: 'ipad12,2'; Name: 'iPad 9th Gen (WiFi+Cellular)'),
(Machine: 'ipad13,1'; Name: 'iPad Air 4th Gen (WiFi)'),
(Machine: 'ipad13,10'; Name: 'iPad Pro 12.9 inch 5th Gen'),
(Machine: 'ipad13,11'; Name: 'iPad Pro 12.9 inch 5th Gen'),
(Machine: 'ipad13,16'; Name: 'iPad Air 5th Gen (WiFi)'),
(Machine: 'ipad13,17'; Name: 'iPad Air 5th Gen (WiFi+Cellular)'),
(Machine: 'ipad13,2'; Name: 'iPad Air 4th Gen (WiFi+Cellular)'),
(Machine: 'ipad13,4'; Name: 'iPad Pro 11 inch 5th Gen'),
(Machine: 'ipad13,5'; Name: 'iPad Pro 11 inch 5th Gen'),
(Machine: 'ipad13,6'; Name: 'iPad Pro 11 inch 5th Gen'),
(Machine: 'ipad13,7'; Name: 'iPad Pro 11 inch 5th Gen'),
(Machine: 'ipad13,8'; Name: 'iPad Pro 12.9 inch 5th Gen'),
(Machine: 'ipad13,9'; Name: 'iPad Pro 12.9 inch 5th Gen'),
(Machine: 'ipad14,1'; Name: 'iPad mini 6th Gen (WiFi)'),
(Machine: 'ipad14,2'; Name: 'iPad mini 6th Gen (WiFi+Cellular)'),
(Machine: 'ipad2,1'; Name: '2nd Gen iPad'),
(Machine: 'ipad2,2'; Name: '2nd Gen iPad GSM'),
(Machine: 'ipad2,3'; Name: '2nd Gen iPad CDMA'),
(Machine: 'ipad2,4'; Name: '2nd Gen iPad New Revision'),
(Machine: 'ipad2,5'; Name: 'iPad mini'),
(Machine: 'ipad2,6'; Name: 'iPad mini GSM+LTE'),
(Machine: 'ipad2,7'; Name: 'iPad mini CDMA+LTE'),
(Machine: 'ipad3,1'; Name: '3rd Gen iPad'),
(Machine: 'ipad3,2'; Name: '3rd Gen iPad CDMA'),
(Machine: 'ipad3,3'; Name: '3rd Gen iPad GSM'),
(Machine: 'ipad3,4'; Name: '4th Gen iPad'),
(Machine: 'ipad3,5'; Name: '4th Gen iPad GSM+LTE'),
(Machine: 'ipad3,6'; Name: '4th Gen iPad CDMA+LTE'),
(Machine: 'ipad4,1'; Name: 'iPad Air (WiFi)'),
(Machine: 'ipad4,2'; Name: 'iPad Air (GSM+CDMA)'),
(Machine: 'ipad4,3'; Name: '1st Gen iPad Air (China)'),
(Machine: 'ipad4,4'; Name: 'iPad mini Retina (WiFi)'),
(Machine: 'ipad4,5'; Name: 'iPad mini Retina (GSM+CDMA)'),
(Machine: 'ipad4,6'; Name: 'iPad mini Retina (China)'),
(Machine: 'ipad4,7'; Name: 'iPad mini 3 (WiFi)'),
(Machine: 'ipad4,8'; Name: 'iPad mini 3 (GSM+CDMA)'),
(Machine: 'ipad4,9'; Name: 'iPad Mini 3 (China)'),
(Machine: 'ipad5,1'; Name: 'iPad mini 4 (WiFi)'),
(Machine: 'ipad5,2'; Name: '4th Gen iPad mini (WiFi+Cellular)'),
(Machine: 'ipad5,3'; Name: 'iPad Air 2 (WiFi)'),
(Machine: 'ipad5,4'; Name: 'iPad Air 2 (Cellular)'),
(Machine: 'ipad6,11'; Name: 'iPad (2017)'),
(Machine: 'ipad6,12'; Name: 'iPad (2017)'),
(Machine: 'ipad6,3'; Name: 'iPad Pro (9.7 inch, WiFi)'),
(Machine: 'ipad6,4'; Name: 'iPad Pro (9.7 inch, WiFi+LTE)'),
(Machine: 'ipad6,7'; Name: 'iPad Pro (12.9 inch, WiFi)'),
(Machine: 'ipad6,8'; Name: 'iPad Pro (12.9 inch, WiFi+LTE)'),
(Machine: 'ipad7,1'; Name: 'iPad Pro 2nd Gen (WiFi)'),
(Machine: 'ipad7,11'; Name: 'iPad 7th Gen 10.2-inch (WiFi)'),
(Machine: 'ipad7,12'; Name: 'iPad 7th Gen 10.2-inch (WiFi+Cellular)'),
(Machine: 'ipad7,2'; Name: 'iPad Pro 2nd Gen (WiFi+Cellular)'),
(Machine: 'ipad7,3'; Name: 'iPad Pro 10.5-inch 2nd Gen'),
(Machine: 'ipad7,4'; Name: 'iPad Pro 10.5-inch 2nd Gen'),
(Machine: 'ipad7,5'; Name: 'iPad 6th Gen (WiFi)'),
(Machine: 'ipad7,6'; Name: 'iPad 6th Gen (WiFi+Cellular)'),
(Machine: 'ipad8,1'; Name: 'iPad Pro 11 inch 3rd Gen (WiFi)'),
(Machine: 'ipad8,10'; Name: 'iPad Pro 11 inch 4th Gen (WiFi+Cellular)'),
(Machine: 'ipad8,11'; Name: 'iPad Pro 12.9 inch 4th Gen (WiFi)'),
(Machine: 'ipad8,12'; Name: 'iPad Pro 12.9 inch 4th Gen (WiFi+Cellular)'),
(Machine: 'ipad8,2'; Name: 'iPad Pro 11 inch 3rd Gen (1TB, WiFi)'),
(Machine: 'ipad8,3'; Name: 'iPad Pro 11 inch 3rd Gen (WiFi+Cellular)'),
(Machine: 'ipad8,4'; Name: 'iPad Pro 11 inch 3rd Gen (1TB, WiFi+Cellular)'),
(Machine: 'ipad8,5'; Name: 'iPad Pro 12.9 inch 3rd Gen (WiFi)'),
(Machine: 'ipad8,6'; Name: 'iPad Pro 12.9 inch 3rd Gen (1TB, WiFi)'),
(Machine: 'ipad8,7'; Name: 'iPad Pro 12.9 inch 3rd Gen (WiFi+Cellular)'),
(Machine: 'ipad8,8'; Name: 'iPad Pro 12.9 inch 3rd Gen (1TB, WiFi+Cellular)'),
(Machine: 'ipad8,9'; Name: 'iPad Pro 11 inch 4th Gen (WiFi)'),
(Machine: 'iphone1,1'; Name: 'iPhone'),
(Machine: 'iphone1,2'; Name: 'iPhone 3G'),
(Machine: 'iphone10,1'; Name: 'iPhone 8'),
(Machine: 'iphone10,2'; Name: 'iPhone 8 Plus'),
(Machine: 'iphone10,3'; Name: 'iPhone X Global'),
(Machine: 'iphone10,4'; Name: 'iPhone 8'),
(Machine: 'iphone10,5'; Name: 'iPhone 8 Plus'),
(Machine: 'iphone10,6'; Name: 'iPhone X GSM'),
(Machine: 'iphone11,2'; Name: 'iPhone XS'),
(Machine: 'iphone11,4'; Name: 'iPhone XS Max'),
(Machine: 'iphone11,6'; Name: 'iPhone XS Max Global'),
(Machine: 'iphone11,8'; Name: 'iPhone XR'),
(Machine: 'iphone12,1'; Name: 'iPhone 11'),
(Machine: 'iphone12,3'; Name: 'iPhone 11 Pro'),
(Machine: 'iphone12,5'; Name: 'iPhone 11 Pro Max'),
(Machine: 'iphone12,8'; Name: 'iPhone SE 2nd Gen'),
(Machine: 'iphone13,1'; Name: 'iPhone 12 Mini'),
(Machine: 'iphone13,2'; Name: 'iPhone 12'),
(Machine: 'iphone13,3'; Name: 'iPhone 12 Pro'),
(Machine: 'iphone13,4'; Name: 'iPhone 12 Pro Max'),
(Machine: 'iphone14,2'; Name: 'iPhone 13 Pro'),
(Machine: 'iphone14,3'; Name: 'iPhone 13 Pro Max'),
(Machine: 'iphone14,4'; Name: 'iPhone 13 Mini'),
(Machine: 'iphone14,5'; Name: 'iPhone 13'),
(Machine: 'iphone14,6'; Name: 'iPhone SE 3rd Gen'),
(Machine: 'iphone14,7'; Name: 'iPhone 14'),
(Machine: 'iphone14,8'; Name: 'iPhone 14 Plus'),
(Machine: 'iphone15,2'; Name: 'iPhone 14 Pro'),
(Machine: 'iphone15,3'; Name: 'iPhone 14 Pro Max'),
(Machine: 'iphone2,1'; Name: 'iPhone 3GS'),
(Machine: 'iphone3,1'; Name: 'iPhone 4'),
(Machine: 'iphone3,2'; Name: 'iPhone 4 GSM Rev A'),
(Machine: 'iphone3,3'; Name: 'iPhone 4 CDMA'),
(Machine: 'iphone4,1'; Name: 'iPhone 4S'),
(Machine: 'iphone5,1'; Name: 'iPhone 5 (GSM)'),
(Machine: 'iphone5,2'; Name: 'iPhone 5 (GSM+CDMA)'),
(Machine: 'iphone5,3'; Name: 'iPhone 5C (GSM)'),
(Machine: 'iphone5,4'; Name: 'iPhone 5C (Global)'),
(Machine: 'iphone6,1'; Name: 'iPhone 5S (GSM)'),
(Machine: 'iphone6,2'; Name: 'iPhone 5S (Global)'),
(Machine: 'iphone7,1'; Name: 'iPhone 6 Plus'),
(Machine: 'iphone7,2'; Name: 'iPhone 6'),
(Machine: 'iphone8,1'; Name: 'iPhone 6s'),
(Machine: 'iphone8,2'; Name: 'iPhone 6s Plus'),
(Machine: 'iphone8,4'; Name: 'iPhone SE (GSM)'),
(Machine: 'iphone9,1'; Name: 'iPhone 7'),
(Machine: 'iphone9,2'; Name: 'iPhone 7 Plus'),
(Machine: 'iphone9,3'; Name: 'iPhone 7'),
(Machine: 'iphone9,4'; Name: 'iPhone 7 Plus'),
(Machine: 'ipod1,1'; Name: '1st Gen iPod'),
(Machine: 'ipod2,1'; Name: '2nd Gen iPod'),
(Machine: 'ipod3,1'; Name: '3rd Gen iPod'),
(Machine: 'ipod4,1'; Name: '4th Gen iPod'),
(Machine: 'ipod5,1'; Name: '5th Gen iPod'),
(Machine: 'ipod7,1'; Name: '6th Gen iPod'),
(Machine: 'ipod9,1'; Name: '7th Gen iPod'),
(Machine: 'watch1,1'; Name: 'Apple Watch 38mm case'),
(Machine: 'watch1,2'; Name: 'Apple Watch 42mm case'),
(Machine: 'watch2,3'; Name: 'Apple Watch Series 2 38mm case'),
(Machine: 'watch2,4'; Name: 'Apple Watch Series 2 42mm case'),
(Machine: 'watch2,6'; Name: 'Apple Watch Series 1 38mm case'),
(Machine: 'watch2,7'; Name: 'Apple Watch Series 1 42mm case'),
(Machine: 'watch3,1'; Name: 'Apple Watch Series 3 38mm case (GPS+Cellular)'),
(Machine: 'watch3,2'; Name: 'Apple Watch Series 3 42mm case (GPS+Cellular)'),
(Machine: 'watch3,3'; Name: 'Apple Watch Series 3 38mm case (GPS)'),
(Machine: 'watch3,4'; Name: 'Apple Watch Series 3 42mm case (GPS)'),
(Machine: 'watch4,1'; Name: 'Apple Watch Series 4 40mm case (GPS)'),
(Machine: 'watch4,2'; Name: 'Apple Watch Series 4 44mm case (GPS)'),
(Machine: 'watch4,3'; Name: 'Apple Watch Series 4 40mm case (GPS+Cellular)'),
(Machine: 'watch4,4'; Name: 'Apple Watch Series 4 44mm case (GPS+Cellular)'),
(Machine: 'watch5,1'; Name: 'Apple Watch Series 5 40mm case (GPS)'),
(Machine: 'watch5,10'; Name: 'Apple Watch SE 44mm case (GPS)'),
(Machine: 'watch5,11'; Name: 'Apple Watch SE 40mm case (GPS+Cellular)'),
(Machine: 'watch5,12'; Name: 'Apple Watch SE 44mm case (GPS+Cellular)'),
(Machine: 'watch5,2'; Name: 'Apple Watch Series 5 44mm case (GPS)'),
(Machine: 'watch5,3'; Name: 'Apple Watch Series 5 40mm case (GPS+Cellular)'),
(Machine: 'watch5,4'; Name: 'Apple Watch Series 5 44mm case (GPS+Cellular)'),
(Machine: 'watch5,9'; Name: 'Apple Watch SE 40mm case (GPS)'),
(Machine: 'watch6,1'; Name: 'Apple Watch Series 6 40mm case (GPS)'),
(Machine: 'watch6,10'; Name: 'Apple Watch SE 40mm case (GPS)'),
(Machine: 'watch6,11'; Name: 'Apple Watch SE 44mm case (GPS)'),
(Machine: 'watch6,12'; Name: 'Apple Watch SE 40mm case (GPS+Cellular)'),
(Machine: 'watch6,13'; Name: 'Apple Watch SE 44mm case (GPS+Cellular)'),
(Machine: 'watch6,14'; Name: 'Apple Watch Series 8 40mm case (GPS)'),
(Machine: 'watch6,15'; Name: 'Apple Watch Series 8 44mm case (GPS)'),
(Machine: 'watch6,16'; Name: 'Apple Watch Series 8 40mm case (GPS+Cellular)'),
(Machine: 'watch6,17'; Name: 'Apple Watch Series 8 44mm case (GPS+Cellular)'),
(Machine: 'watch6,18'; Name: 'Apple Watch Ultra'),
(Machine: 'watch6,2'; Name: 'Apple Watch Series 6 44mm case (GPS)'),
(Machine: 'watch6,3'; Name: 'Apple Watch Series 6 40mm case (GPS+Cellular)'),
(Machine: 'watch6,4'; Name: 'Apple Watch Series 6 44mm case (GPS+Cellular)'),
(Machine: 'watch6,6'; Name: 'Apple Watch Series 7 41mm case (GPS)'),
(Machine: 'watch6,7'; Name: 'Apple Watch Series 7 45mm case (GPS)'),
(Machine: 'watch6,8'; Name: 'Apple Watch Series 7 41mm case (GPS+Cellular)'),
(Machine: 'watch6,9'; Name: 'Apple Watch Series 7 45mm case (GPS+Cellular)'),
(Machine: 'x86_64'; Name: 'iPhone Simulator'));
var
LDevice: TAppleDevice;
LIndex: Integer;
LStringComparer: IComparer<string>;
LMachine: string;
begin
LMachine := GetDeviceModelName;
LDevice.Machine := LMachine.ToLower;
LDevice.Name := '';
LStringComparer := TComparer<string>.Default;
if TArray.BinarySearch<TAppleDevice>(AppleDevices, LDevice, LIndex, TComparer<TAppleDevice>.Construct(
function(const ALeft, ARight: TAppleDevice): Integer
begin
Result := LStringComparer.Compare(ALeft.Machine, ARight.Machine);
end)) then
begin
Result := AppleDevices[LIndex].Name;
end
else if LMachine.IsEmpty then
Result := ''
else
begin
{$IFDEF DEBUG}
for LIndex := 0 to Length(AppleDevices)-1 do
if (AppleDevices[LIndex].Machine <> AppleDevices[LIndex].Machine.ToLower) then
raise Exception.Create('Apple device list is wrong (the "Machine" field don''t allow upper case)');
for LIndex := 1 to Length(AppleDevices)-1 do
if LStringComparer.Compare(AppleDevices[LIndex-1].Machine, AppleDevices[LIndex].Machine) <> -1 then
raise Exception.Create('Apple device list is wrong (it isn''t ordered or have duplicate item)');
{$ENDIF}
Result := '[' + LMachine + ']';
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment