【问题标题】:Xcode detect iOS version and show storyboard accordinglyXcode 检测 iOS 版本并相应地显示情节提要
【发布时间】:2013-10-11 17:52:52
【问题描述】:

我想向我的 iOS6 用户和 iOS7 用户展示一个故事板。我该怎么做?

【问题讨论】:

    标签: ios xcode storyboard ios7


    【解决方案1】:
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        //load and show ios6 storyboard
    }
    else {
        //load and show ios7 storyboard
    }
    

    【讨论】:

    • 在 iOS 6.1.3 上无法正常工作,因为对于 6.1.3 iOS 版本,它的浮点值是 993.0 而 NSFoundationVersionNumber_iOS_6_1 是 992.0 som 对于 6.1.3 版本你会错误地假设它是 iOS 7 或更大
    • @NikolayShubenkov - 我从 iOS7 过渡指南中获取了这段代码。 (developer.apple.com/library/prerelease/ios/documentation/…
    • @NikolayShubenkov - 有趣的是我看到了... #define NSFoundationVersionNumber_iOS_6_0 993.00 #define NSFoundationVersionNumber_iOS_6_1 993.00 in NSObjCRuntime.h
    • 其实我不是故意调查这个的,而是当我的应用在 iOS 6.1.3 设备上崩溃时。崩溃是因为通过这种方法检查 iOS 版本后,系统检测到它的版本类似于 iOS 7 或更高版本。然后我在运行时检查了这个 NSFoundationVersionNumber 值
    【解决方案2】:

    您可以使用此代码获取 iOS 版本:

    [[UIDevice currentDevice] systemVersion]
    

    例如,要检测 iOS 6,您可以执行以下操作:

    if ([[UIDevice currentDevice].systemVersion hasPrefix:@"6"]) {
        // ...
    }
    

    然后,要为 iOS 6 和 7 加载不同的故事板,您可以执行以下操作:

    if ([[UIDevice currentDevice].systemVersion hasPrefix:@"6"]) {
        myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_6" bundle:nil];
    } else {
        myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_7" bundle:nil];
    }
    

    编辑: 如其他答案所述,检测 iOS 版本的一种可以说是更好的方法是使用 NSFoundationVersionNumber,因为不需要对 systemVersion 进行字符串解析。

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_6" bundle:nil];
    } else {
        myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_7" bundle:nil];
    }
    

    【讨论】:

      【解决方案3】:

      您可以在 AppDelegate 中尝试这样的事情(非常重要)

       UIStoryboard *storyboard = nil; 
       if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
          storyboard =  [UIStoryboard storyboardWithName:@"iOS7_AND_ABOVE" bundle:[NSBundle mainBundle]];
       } else {
          storyboard =  [UIStoryboard storyboardWithName:@"iOS_below_7" bundle:[NSBundle mainBundle]];
       }
      

      【讨论】:

      • Xcode 似乎不知道 NSFoundationVersionNumber_iOS_6_1 是什么。它最多只知道 5_1。那是怎么回事?
      • @Chase Robers 为此使用 iOS SDK 7。 NSFoundationVersionNumber_iOS_6_1 仅在 IOS 7 SDK 上定义
      • 我的 mac 太旧了,无法让我将操作系统升级到 xcode 更新所需的最低版本,所以我运气不好......
      • 将无法在 iOS 6.1.3 上正常工作,因为它的浮动基础值大于 NSFoundationVersionNumber_iOS_6_1
      猜你喜欢
      • 2015-06-13
      • 2012-05-18
      • 2012-06-08
      • 2019-03-01
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      相关资源
      最近更新 更多