Sometimes it can be convenient to be able to detect when you are running inside the simulator or when you are running on a device. For example, suppose you have a service which supplies data it might be useful when you want to retrieve data from a testing server instead of the production server when you are running in the simulator.
The check below works for both the iPhone / iPad, however, keep in mind that this is only a compile time check, so the code included in the binary depends on your compile target.
The detection mechanism is very simple:
#if TARGET_IPHONE_SIMULATOR
// We are running inside the simulator
#else
// We are running on a device
#endif
If you only want to check if you are running on a device you can use the following mechanism:
When you are creating an iPhone / iPad application that requires a Wifi connection, or when you want to change your applications behavior when you are working on a Wifi connection or 3G connection you need to be able to check if you are connected to a Wifi network.
Behavior difference can for example be if you want to retrieve the ten latest headlines or fifty latest headlines, a Wifi connection can be fast enough to retrieve more data in the same time window as you would using a 3G connection, which could enable a better user experience.
The following code samples require the use of the Reachability class provided by Apple, you can download the class from the following location:
Reachability code
The following code sample checks for the existence of a Wifi connection:
if ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus]
==
… Read more >
Recent Comments