tags:

views:

129

answers:

3
  1. How can I detect whether my app is running on an iPhone or iPad, and what OS version it is running?
  2. Can I use pre-processor macros in a similar fashion to #if _OS4.0 or #if IPAD or something of the sort?
  3. Is this kosher, or should I just make separate builds for submission?
A: 

The UI_USER_INTERFACE_IDIOM() can be used to detect the device. Possible return values are:

  • UIUserInterfaceIdiomPhone
  • UIUserInterfaceIdiomPad

if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {}

There's also the __IPHONE_3_2 define that can help:

#ifndef __IPHONE_3_2
    /* ... */
#elif
   /* ... */
#endif
Macmade
This is very helpful thank you. I've run into some other issues though. When I try to build with my iPad target, all of the code for my iAd stuff breaks.
devMeSilus
+2  A: 

Generally using features based on OS version is bad practice.

As offered, use UI_USER_INTERFACE_IDIOM() whenever possible. Also use:

Class qlPreview = NSClassFromString(@"QLPreviewController");

and

UIScreen *mainScreen = [UIScreen mainScreen];
if([mainScreen respondsToSelector:@selector(scale)]){
     NSLog(@"screen scale: %f",[mainScreen scale]);
}

However, sometimes you can't avoid it checking for system version.

christo16
Note that `UISwipeGestureRecognizer` and `addGestureRecognizer:` exist even before 3.2 — they are just being private.
KennyTM
Right, bad example :) Updated.
christo16