6

I'm using home screen quick actions that's only supported in IOS9. Using the constant UIApplicationLaunchOptionsShortcutItemKey will crash if used in IOS8. What is the correct way to check if quick actions is supported?

One way is to check for IOS9 through systemVersion but I'm hoping there is a better way. [[UIDevice currentDevice] systemVersion]

Nikunj
  • 655
  • 3
  • 13
  • 25
Toydor
  • 2,277
  • 4
  • 30
  • 48

3 Answers3

8

In objective C you can check to see if a class exists. Say something like

if([UIApplicationShortcutItem class]){
//Handle shortcut launch
}
beyowulf
  • 15,101
  • 2
  • 34
  • 40
2

I think in case of Swift the best way for checking the API availability is Automatic operating system API availability checking that's new feature released with iOS9 and Swift2

if #available(iOS 9, *) {
    // use UIApplicationLaunchOptionsShortcutItemKey
} else {
    // no available
}

#available is going to check whether are we using iOS 9 or later, or any other unknown platforms like watchOS so the * is also here.

If your code is inside a function then you can use #available with guard like this.

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

Mark your methods and class as well like

@available(iOS 9, *)
func useMyStackView() {
    // use UIStackView
}

@available works similarly to #available so If your deployment target is iOS7 or less than 9, you can't call that useMyStackView()

Buntylm
  • 7,345
  • 1
  • 31
  • 51
0

For Objc, it also can use @available (iOS 9, *) to check the OS version

if (@available(iOS 9, *)) {
    //use UIApplicationLaunchOptionsShortcutItemKey
}
Paul.Chou
  • 3
  • 4