-1

I have some logic in my iOS application that I would like to execute differently when testing on an the iPhone simulator vs when its running on a live device.

Is there any way to determine in objective C whether the logic is being executed on one or the other?

Currently, I comment out some code before I deploy to my physical iPhone. Not convenient.

The reason behind the (slightly) different execution paths btw, is that my application utilizes data that is time/date dependent. On the simulator i have a static data set loaded so my testing takes that into account (i.e doesnt use current systems dates etc).

On the live device, the data is always fresh so no such issues.

iansari
  • 354
  • 3
  • 12
  • Thanks. Interestingly I didn't find that answer when i searched earlier. Should have searched harder :-) – iansari May 01 '13 at 13:23

3 Answers3

1

It really should be known at compile time, as per TARGET_IPHONE_SIMULATOR macro. If you need to do a runtime check:

bool is_simulator()
{
  NSString *model = [[UIDevice currentDevice] model];
  return [model isEqualToString:@"iPhone Simulator"];
}

or without using objective C you could perhaps use sysctl as follows:

static int32_t sysctl_int32(const char* name)
{
  int32_t val = 0;
  size_t size = sizeof(val);

  sysctlbyname(name, &val, &size, NULL, 0);

  return val;
}

bool is_simulator()
{
  int32_t cpu_type = sysctl_int32("hw.cputype");
  return (cpu_type == CPU_TYPE_I386 || cpu_type == CPU_TYPE_X86_64)
}
robbie_c
  • 2,428
  • 1
  • 19
  • 28
0

Try

if (TARGET_IPHONE_SIMULATOR){
     //Running on simulator
}else{
     //Real one
}
Anupdas
  • 10,211
  • 2
  • 35
  • 60
0

Use

#if TARGET_IPHONE_SIMULATOR
    // Using Simulator
#else
// Using device
Linuxios
  • 34,849
  • 13
  • 91
  • 116
Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61