检查快速操作可用性

时间:2015-11-05 12:19:59

标签: ios objective-c iphone 3dtouch

我正在使用IOS9仅支持的主屏幕快速操作。 如果在UIApplicationLaunchOptionsShortcutItemKey中使用常量IOS8将会崩溃。 检查是否支持快速操作的正确方法是什么?

一种方法是通过systemVersion检查IOS9,但我希望有更好的方法。 [[UIDevice currentDevice] systemVersion]

3 个答案:

答案 0 :(得分:8)

在目标C中,您可以检查是否存在类。说出类似

的内容
if([UIApplicationShortcutItem class]){
//Handle shortcut launch
}

答案 1 :(得分:2)

我认为在Swift的情况下,检查API可用性的最佳方法是Automatic operating system API availability checkingiOS9和{{1}一起发布的新功能}

Swift2

if #available(iOS 9, *) { // use UIApplicationLaunchOptionsShortcutItemKey } else { // no available } 将检查我们是使用iOS 9或更高版本,还是使用#available等任何其他未知平台,以便watchOS也在此处。

如果您的代码位于某个函数中,那么您可以*使用#available这样的代码。

guard

标记你的方法和课程,如

guard #available(iOS 9, *) else {
    return
}

@available(iOS 9, *) func useMyStackView() { // use UIStackView } @available的工作方式类似,因此,如果您的部署目标为#available或小于9,则无法调用iOS7

答案 2 :(得分:0)

对于Objc,它也可以使用@available (iOS 9, *)来检查操作系统版本

if (@available(iOS 9, *)) {
    //use UIApplicationLaunchOptionsShortcutItemKey
}
相关问题