【问题标题】:conditional add code for iOS depending on the device iOS根据设备 iOS 的 iOS 条件添加代码
【发布时间】:2015-11-30 13:58:06
【问题描述】:

我想使用以下代码,但仅适用于在 iOS

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if(..)
      test()

    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

}

#endif

但是,当我构建代码并在设备上运行它时,该方法不会执行。我做错了什么?该应用程序最低支持 iOS 7.0,也可以在 iOS9 中运行(最新的 xcode 7)

【问题讨论】:

  • 你不能像这样使用预处理器宏,因为这是一个编译时特性。您需要运行时检查。
  • 为什么不希望为 iOS 9.0 及更高版本调用此委托方法?
  • Maddy 是对的——预处理器检查不是在运行时进行的。所以你的版本要么有这个功能,要么没有。您需要像 trojanfoe 一样的运行时检查。

标签: ios ios9


【解决方案1】:

使用此“系统版本控制预处理器宏”检查系统版本

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

【讨论】:

  • iOS9 应该用什么代替 v? 9000?
  • @cateof if (SYSTEM_VERSION_LESS_THAN(@"9.0")) 这样你可以使用
  • 这不是正确的答案。我问我是否可以使用预处理器宏来做到这一点。如果我的设备在 iOS9 上运行,我根本不希望触发任何方法。
【解决方案2】:

您需要删除 #if __IPHONE_OS_VERSION_MAX_ALLOWED &lt; 90000#endif,然后我认为这样做可以:

float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version < 9.0f)
    test()

【讨论】:

  • #if #else 预处理器宏有什么问题?
  • 我认为您希望在每个版本中运行相同的代码?
  • 不,我不想在 iOS9 中触发 supportInterfaceOrientationsForWindow
猜你喜欢
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
相关资源
最近更新 更多