__IPHONE_OS_VERSION_MAX_ALLOWED有哪些常见用例? [英] What are the common use cases for __IPHONE_OS_VERSION_MAX_ALLOWED?

查看:95
本文介绍了__IPHONE_OS_VERSION_MAX_ALLOWED有哪些常见用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将使用__IPHONE_OS_VERSION_MAX_ALLOWED支票的情况是什么?怎么样__IPHONE_OS_VERSION_MIN_REQUIRED?

What are the situations in which you would use the __IPHONE_OS_VERSION_MAX_ALLOWED check? What about __IPHONE_OS_VERSION_MIN_REQUIRED?

推荐答案

重要的是要理解这些是编译时常量,它们是因此无法在运行时检测您正在运行的平台或操作系统版本(例如,检测您是否在iPad和iPhone上运行)。

It's important to understand that these are compile-time constants, they are therefore not useful for detecting at runtime what platform or OS version you are running on (e.g. detecting if you are running on iPad vs iPhone).

这些常量的作用是允许的您可以在编译时检测是否为给定的SDK或部署目标构建代码。例如,如果您编写的开源库包含仅在针对iOS 5 SDK编译时才有效的代码,则可以包含此检查以检测编译代码的SDK:

What these constants do is allow you to detect at compile time whether the code is being built for a given SDK or deployment target. For example, if you wrote an open source library that contains code that only works when compiled against the iOS 5 SDK, you might include this check to detect which SDK the code is being compiled for:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 50000
    //you can use iOS 5 APIs here because the SDK supports them
    //but the code may still crash if run on an iOS 4 device
#else
    //this code can't use iOS 5 APIs as the SDK version doesn't support them
#endif

或者,如果你想看看目标的最低操作系统版本是什么......

Or alternatively, if you want to see what the minimum OS version being targeted is...

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 50000
    //minimum deployment target is 5.0, so it's safe to use iOS 5-only code
#else
    //you can use iOS5 APIs, but the code will need to be backwards
    //compatible or it will crash when run on an iOS 4 device
#endif

Thi s与在运行时检测您正在运行的操作系统不同。如果您使用iOS 4 SDK在上面的第一个示例中编译代码,它将使用您的iOS 4安全代码,但在iOS 5设备上运行时不会利用任何iOS 5功能。如果您使用iOS 5 SDK构建它,然后将部署目标设置为iOS 4并尝试在iOS 4设备上运行它,它将编译并安装正常但仍可能在运行时崩溃,因为iOS 5 API不存在。

This is different from detecting at runtime what OS you are running on. If you compile the code in the first example above using the iOS 4 SDK it will use your iOS 4-safe code, but won't take advantage of any iOS 5 features when run on an iOS 5 device. If you build it using the iOS 5 SDK then set the deployment target to iOS 4 and try to run it on an iOS 4 device, it will compile and install fine but may still crash at runtime because the iOS 5 APIs aren't there.

在上面的第二个示例中,如果您将部署目标设置为iOS 4或更低版本,那么它将使用iOS 4安全代码路径,但是如果您设置部署目标到iOS 5,它将无法在iOS 4设备上运行(它将拒绝安装)。

In the second example above, if you set your deployment target to iOS 4 or below then it will use the iOS 4-safe code path, but if you set the deployment target to iOS 5, it won't run at all on an iOS 4 device (it will refuse to install).

构建在iOS 4上运行的应用程序5并且仍然可以利用iOS 5的功能,如果它们可用,则需要进行运行时检测。要在运行时检测iOS版本,您可以这样做:

To build an app that runs on iOS 4 and 5 and is still able to take advantage of iOS 5 features if they are available, you need to do run-time detection. To detect the iOS version at runtime you can do this:

if ([[[UIDevice currentDevice] systemVersion] compare:@"5.0.1" options:NSNumericSearch] != NSOrderedAscending) {
    //running on iOS 5.0.1 or higher
}

但这意味着要准确跟踪哪些操作系统版本中添加了哪些API功能,这些功能很笨重,只能作为最后的手段来完成。通常,更好的方法是使用特征检测,如下所示:

But that means keeping track of exactly which API features were added in which OS version, which is clunky and should only be done as a last resort. Usually, a better approach is to use feature detection, like this:

if ([SomeClass class]) {
    //this class exists
}

if ([SomeClass instancesRespondToSelector:@selector(someMethod:)]) {
    //this method exists
}

此外,要在运行时检测您是否在iPad或iPhone上,您可以这样做:

Also, to detect at runtime if you are on an iPad or iPhone, you can do this:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    //on an ipad
}

在运行时执行这些检查可以创建在多个设备和iOS版本上运行的单个应用程序,并且可以利用每个平台的功能。

Performing these checks at runtime allows you to create a single app that runs on multiple devices and iOS versions and is able to take advantage of the features of each platform.

这篇关于__IPHONE_OS_VERSION_MAX_ALLOWED有哪些常见用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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