Jus*_*ers 10 iphone objective-c c-preprocessor
是否有我可以检查的构建预处理器宏,用#if或#ifdef来确定我当前的Xcode项目是否是为iPhone或iPad构建的?
编辑
正如几个答案所指出的,通常应用程序是通用的,并且相同的二进制文件可以在两个设备上运行.这些非常相似的设备之间的条件行为应该在运行时而不是编译时解决.
Lou*_*nco 25
本博客评论部分的一些想法
http://greensopinion.blogspot.com/2010/04/from-iphone-to-ipad-creating-universal.html
主要使用
UI_USER_INTERFACE_IDIOM()
Run Code Online (Sandbox Code Playgroud)
如:
#ifdef UI_USER_INTERFACE_IDIOM()
#define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#else
#define IS_IPAD() (false)
#endif
Run Code Online (Sandbox Code Playgroud)
小智 9
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPhone"]) {
//iPhone
}
else if([deviceType isEqualToString:@"iPod touch"]) {
//iPod Touch
}
else {
//iPad
}
Run Code Online (Sandbox Code Playgroud)
就我而言,您不能使用#if或#ifdef来执行此操作,但是,它受支持,因为Obj-C是C的严格超集.
相关: 使用iPhone SDK确定设备(iPhone,iPod Touch)
无法确定您的应用是否专为iPhone或iPad构建.#if在构建期间解析预处理器指令.一旦您的应用程序构建并标记为通用,它必须在两个设备上正确运行.在构建期间,没有人知道稍后将在何处安装,并且可以在两者上安装一个构建.
但是,您可能想要执行以下操作之一:
在运行时检测设备模型.
要做到这一点,使用[[UIDevice currentDevice] model]和比较iPhone,iPod touch或iPad字符串.即使在iPad 上以兼容模式运行(仅适用于iPhone的应用程序),这也会返回正确的设备.这对于使用情况分析非常有用.
在运行时检测用户界面习语.
这是每个人在为iPhone和iPad提供不同内容时检查的内容.使用[[UIDevice currentDevice] userInterfaceIdiom]和比较UIUserInterfaceIdiomPhone或UIUserInterfaceIdiomPad.您可能想要制作这样的便利方法:
@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
Run Code Online (Sandbox Code Playgroud)
然后你可以使用:
if ([[UIDevice currentDevice] iPhone]) { }
// or
if ([UIDevice iPhone]) { }
// or
if (UIDevice.iPhone) { }
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
21730 次 |
| 最近记录: |