Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for quick actions availability

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]

like image 935
Toydor Avatar asked Nov 05 '15 12:11

Toydor


2 Answers

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

if([UIApplicationShortcutItem class]){
//Handle shortcut launch
}
like image 53
beyowulf Avatar answered Oct 21 '22 04:10

beyowulf


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()

like image 23
Buntylm Avatar answered Oct 21 '22 06:10

Buntylm