How to check if the app is running on iOS device or simulator
Asked Answered
A

2

7

Based on this link Conditional compilation (Delphi) CPUARM conditional if should be false for Simulator and true for device, the problem is it's not working for me. I am using Delphi XE6, iOS Simulator 7.1

This is my code

    {$IFDEF CPUARM}
s := 'iOS device';
    {$ELSE}
s := 'iOS Simulator';
    {$ENDIF}

p.s iOS Simulator is running in a VMWare virtual machine.

Appendicle answered 22/5, 2014 at 9:37 Comment(2)
You should always add the common delphi tag to your Delphi related questions. Maybe you were wondering for the low attention on your questions. That is the reason :o)Rosati
@RobKennedy, actually, no. The iOS simulator does not run the same binary that is deployed to iOS devices. The simulator runs x86 compiled code, not ARM compiled code. So it is not a single compilation. Checking for CPUARM is the correct way to differentiate between an iOS device and an iOS simulator.Huggins
H
9

Checking for CPUARM is the correct solution. iOS binaries compiled for the simulator are not ARM, they are actually x86. Just make sure to wrap your iOS code with {$IFDEF IOS}:

{$IFDEF IOS}
  {$IFDEF CPUARM}
s := 'iOS device';
  {$ELSE}
s := 'iOS Simulator';
  {$ENDIF}
{$ENDIF}

Delphi uses an ARM compiler for iOS devices, but uses an x86 compiler for the iOS simulator.

The available compiler conditionals are documented on Embarcadero's DocWiki:

Conditional compilation (Delphi) | Predefined Conditionals

CPUARM is defined by the DCCIOSARM compiler (iOS device).

CPU386 and CPUX86 are defined by the DCCIOS32 compiler (iOS simulator).

A look at the conditional values that are physically present in XE6's DCCIOSARM.EXE and DCCIOS32.EXE executable files confirms that:

DCCIOSARM.EXE:

**CPUARM**
DCC
NEXTGEN
AUTOREFCOUNT
WEAKINSTREF
WEAKINTFREF
WEAKREF
EXTERNALLINKER
NATIVECODE
POSIX
POSIX32
MACOS
MACOS32
**IOS**
VER270
CONSOLE
BCB
PIC
UNICODE
CONDITIONALEXPRESSIONS

DCCIOS32.EXE:

**CPU386**
**CPUX86**
DCC
NEXTGEN
AUTOREFCOUNT
WEAKINSTREF
WEAKINTFREF
WEAKREF
NATIVECODE
POSIX
POSIX32
MACOS
MACOS32
**IOS**
ALIGN_STACK
UNDERSCOREIMPORTNAME
PC_MAPPED_EXCEPTIONS
ASSEMBLER
VER270
CONSOLE
BCB
PIC
UNICODE
CONDITIONALEXPRESSIONS

UPDATE: starting with Delphi 11.2, the ARM-based iOS simulator is now supported via the new DCCIOSSIMARM64 compiler. You can use the IOSSIMULATOR conditional to check for that compiler:

{$IFDEF IOS}
  {$IFDEF CPUARM}
    {$IFDEF IOSSIMULATOR}
s := 'iOS Simulator (ARM)';
    {$ELSE}
s := 'iOS device';
    {$ENDIF}
  {$ELSE}
s := 'iOS Simulator (x86)';
  {$ENDIF}
{$ENDIF}
Huggins answered 22/5, 2014 at 15:17 Comment(3)
I had checked the documentation and I used CPUARM and IOS as you write, but surprisingly it didn't work.Appendicle
I assure you, it does. There is no possible way that CPUARM can be defined when compiling for the iOS Simulator, unless something else in your project is defining it manually. As you can see in my answer, CPUARM does not physically exist in DCCIOS32.Huggins
"There is no possible way that CPUARM can be defined when compiling for the iOS Simulator" - that is no longer the case starting in Delphi 11.2, which adds support for the ARM-based simulator via the new DCCIOSSIMARM64 compiler. I have updated my answer.Huggins
A
-1

I found a workaround for this problem:

I defined a ISSIM condition for ALL Configuration - IOS Simulator Platform in Project->Options page , then I checked that via this

    {$IFDEF ISSIM}
s := 'iOS Simulator';
    {$ELSE}
s := 'iOS device';
    {$ENDIF}

enter image description here

Appendicle answered 22/5, 2014 at 10:3 Comment(1)
It is not necessary to define your own value, the existing compiler conditionals are sufficient.Huggins

© 2022 - 2024 — McMap. All rights reserved.