Is there开发者_如何学JAVA a build preprocessor macro I can check, with #if
or #ifdef
to determine if my current Xcode project is being built for iPhone or iPad?
EDIT
As several answers have pointed out, often apps are universal, and the same binary can run on both devices. Conditional behavior between these very similar devices should be solved at runtime rather than compile time.
Some ideas in the comment section of this blog
http://greensopinion.blogspot.com/2010/04/from-iphone-to-ipad-creating-universal.html
Mostly using
UI_USER_INTERFACE_IDIOM()
Such as:
#ifdef UI_USER_INTERFACE_IDIOM()
#define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#else
#define IS_IPAD() (false)
#endif
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPhone"]) {
//iPhone
}
else if([deviceType isEqualToString:@"iPod touch"]) {
//iPod Touch
}
else {
//iPad
}
You cannot, as far as I am concerned, use #if or #ifdef to do this but, it is supported because Obj-C is a strict superset of C.
Related: Determine device (iPhone, iPod Touch) with iPhone SDK
There is no way to determine whether your app is built for iPhone or iPad. Preprocessor #if
directives are resolved during build. Once your app is built and flagged as Universal, it has to run correctly on both devices. During building nobody knows where it will be installed later and one build can be installed on both.
However you may want to do one of these:
Detect device model during runtime.
To do this, use
[[UIDevice currentDevice] model]
and compare toiPhone
,iPod touch
oriPad
strings. This will return you correct device even when running in compatibility mode on iPad (for iPhone-only apps). This can be usefull for usage analytics.Detect user interface idiom during runtime.
This is what everyone checks for, when providing different content for iPhone and iPad. Use
[[UIDevice currentDevice] userInterfaceIdiom]
and compare toUIUserInterfaceIdiomPhone
orUIUserInterfaceIdiomPad
. You may want to make convenience methods like this:@implementation UIDevice (UserInterfaceIdiom) - (BOOL)iPhone { return (self.userInterfaceIdiom == UIUserInterfaceIdiomPhone); } + (BOOL)iPhone { return [[UIDevice currentDevice] iPhone]; } - (BOOL)iPad { return (self.userInterfaceIdiom == UIUserInterfaceIdiomPad); } + (BOOL)iPad { return [[UIDevice currentDevice] iPad]; } @end
Then you can use:
if ([[UIDevice currentDevice] iPhone]) { } // or if ([UIDevice iPhone]) { } // or if (UIDevice.iPhone) { }
Updation for swift:
Couldn't use preprocessor. Make global function as
func IS_IPAD() -> Bool {
( return (UIDevice.respondsToSelector(Selector("userInterfaceIdiom"))) && (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad) )}
I use the following code for AppleTV 4 because UIUserInterfaceIdiomUnspecified, doesn't seem to work, nor can I find any other enums:
#ifdef TARGET_OS_IOS
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.width == 1920) {
NSLog(@"tvOS");
}
#endif
I used to use this for iPad and such before the dark times, before the empire- OB1, hah good night. But you can use this similar technique for other screen sizes you know of.
精彩评论