为 iPad 进行编译时是否设置了特定的 Xcode 编译器标志?

发布于 2024-08-19 05:24:43 字数 291 浏览 6 评论 0原文

为 iPad 进行编译时是否设置了特定的 Xcode 编译器标志?

我想有条件地编译 iPad 与 iPhone/iPod Touch 代码,例如:

#ifdef TARGET_IPAD
   code for iPad
#else
   code for iPhone
#endif

我知道 TargetConditionals.h 中已经有 TARGET_OS_IPHONE 和 TARGET_CPU_ARM,但是有什么可以轻松且专门针对 iPad 的吗?

-丽

Is there a specific Xcode compiler flag that gets set when compiling for iPad?

I want to conditionally compile iPad vs iPhone/iPod Touch code for example:

#ifdef TARGET_IPAD
   code for iPad
#else
   code for iPhone
#endif

I know there is already TARGET_OS_IPHONE and TARGET_CPU_ARM in TargetConditionals.h but anything that easily and specifically targets iPad?

-Rei

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

左耳近心 2024-08-26 05:24:43

用于运行时检查 iPad 与 iPhone/iPad Touch 的正确 API 是:

BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);

UIDevice 头文件管理器还包含一个方便的宏 UI_USER_INTERFACE_IDIOM(),如果您的部署目标 <<,这将很有帮助。 iPhone 3.2。

#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)

所以你可以消极地说:

BOOL deviceIsPad = (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone);

The correct API to use for run-time checking of iPad vs. iPhone/iPad Touch is:

BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);

The UIDevice header filer also includes a convenient macro, UI_USER_INTERFACE_IDIOM(), which will be helpful if your deployment target is < iPhone 3.2.

#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)

So you could just say, negatively:

BOOL deviceIsPad = (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone);
辞别 2024-08-26 05:24:43

您应该使用运行时检查,而不是使用编译时标志,例如使用 NSClassFromString 来查看类是否存在,因为相同的应用程序可以安装在两个设备上。

由于可能在两种设备上运行该应用程序,因此没有内置的编译时检查它是否针对 iPad。

Instead of using compile-time flags, you should use run-time check e.g. use NSClassFromString to see if a class exists because the same app can be installed on both devices.

And because of the possibility of running the app in both devices, there isn't a built-in compile-time check whether it targets iPad or not.

涙—继续流 2024-08-26 05:24:43

目前我没有找到任何可以让你检查你是否使用 iPad 的东西,但我也不确定 Apple 是否建议运行时检查。以下是苹果公司的摘录:

除了视图控制器之外,iPhone 和 iPad 设备之间共享的任何类都需要包含条件编译宏以隔离特定于设备的代码。尽管您还可以使用运行时检查来确定特定类或方法是否可用,但这样做只会通过添加在一个设备或另一设备上不会遵循的代码路径来增加可执行文件的大小。让编译器删除此代码有助于保持代码整洁。

但是,我没有地方可以找到有关条件编译宏的更多具体信息。

Currently I didn’t find anything that would let you check if you are on an iPad, but I’m also not sure if Apple recommends runtime checks. Here’s an excerpt from Apple:

In addition to your view controllers, any classes that are shared between iPhone and iPad devices need to include conditional compilation macros to isolate device-specific code. Although you could also use runtime checks to determine if specific classes or methods were available, doing so would only increase the size of your executable by adding code paths that would not be followed on one device or the other. Letting the compiler remove this code helps keep your code cleaner.

However, there is no place I could find more specific information about conditional compilation macros.

三五鸿雁 2024-08-26 05:24:43

对于共享相同项目/代码的多个目标,我通过编辑 iPad 目标的 C 标志来实现此目的。

选择 [myapp]-iPad 目标作为活动目标后,选择 Project ->编辑活动目标 [myapp]-iPad。搜索“c flags”并双击。添加“-D TARGET_IPAD”标志。现在,符号 TARGET_IPAD 将仅为您的 iPad 目标定义。

当然,这仅在您为 iPad 和 iPhone 使用单独的目标时才有效。如果您在两者上运行相同的二进制文件,显然编译器无法为您做任何事情。 (但是,截至一月底的 3.2 SDK 甚至还不支持通用应用程序。)

(已编辑;我对“通用”应用程序等术语感到困惑)

For multiple targets sharing the same project/code, I'm doing this by editing the C flags for my iPad target.

With the [myapp]-iPad target chosen as the active target, pick Project -> Edit Active Target [myapp]-iPad. Search for "c flags" and double-click. Add a flag for "-D TARGET_IPAD". Now the symbol TARGET_IPAD will be defined only for your iPad target.

Of course, this only works if you're using separate targets for iPad and iPhone. If you're running the same binary on both, obviously there's nothing the compiler can do for you. (However, the 3.2 SDK as of the end of January doesn't even support Universal apps yet.)

(Edited; I was confused about the terminology of "Universal" apps etc.)

夏至、离别 2024-08-26 05:24:43

或者->只是为了确定

-(BOOL)isDeviceAniPad
{   
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
 BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
 return deviceIsPad;
#endif
 return NO;
}

Or -> just to be sure

-(BOOL)isDeviceAniPad
{   
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
 BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
 return deviceIsPad;
#endif
 return NO;
}
稚气少女 2024-08-26 05:24:43

我想这会做

-(BOOL)isDeviceAniPad
{   
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
  return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
#endif
  return NO;
}

I think this will do

-(BOOL)isDeviceAniPad
{   
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
  return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
#endif
  return NO;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文