#if check(预处理器宏)来区分iPhone和iPad [英] #if check (preprocessor macro) to differentiate between iPhone and iPad

查看:179
本文介绍了#if check(预处理器宏)来区分iPhone和iPad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有我可以检查的构建预处理器宏,使用 #if 或# ifdef 来确定我当前是否Xcode项目是为iPhone或iPad构建的吗?

Is there 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?

编辑

答案已经指出,通常应用程序是通用的,并且相同的二进制文件可以在两个设备上运行。这些非常相似的设备之间的条件行为应该在运行时而不是编译时解决。

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.

推荐答案

没有办法确定您的应用是否适用于iPhone或iPad。预处理器 #if 指令在构建期间解析。构建应用程序并将其标记为通用后,它必须在两台设备上正确运行。在构建期间,没有人知道它将在以后安装在哪里,并且可以在两者上安装一个版本。

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.

但是你可能想要做其中一个:


  1. 在运行时检测设备型号

为此,请使用 [[UIDevice currentDevice] model] 并与 iPhone iPod touch iPad 字符串。即使在iPad上以兼容模式运行(仅适用于iPhone的应用程序),这也会返回正确的设备。这对于使用情况分析非常有用。

To do this, use [[UIDevice currentDevice] model] and compare to iPhone, iPod touch or iPad 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.

在运行时检测用户界面惯用语

这是大家为iPhone和iPad提供不同内容时所检查的内容。使用 [[UIDevice currentDevice] userInterfaceIdiom] 并与 UIUserInterfaceIdiomPhone UIUserInterfaceIdiomPad 。您可能想要制作这样的便利方法:

This is what everyone checks for, when providing different content for iPhone and iPad. Use [[UIDevice currentDevice] userInterfaceIdiom] and compare to UIUserInterfaceIdiomPhone or UIUserInterfaceIdiomPad. 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

然后你可以使用:

if ([[UIDevice currentDevice] iPhone]) { }
// or
if ([UIDevice iPhone]) { }
// or
if (UIDevice.iPhone) { }


这篇关于#if check(预处理器宏)来区分iPhone和iPad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆