user4385051
user4385051

Reputation:

How to don't start a method in iOS7

How I can let's start the following method only if is >= 8.0 ? It's in the TabBarController class.

let floatVersion = (UIDevice.currentDevice().systemVersion as NSString).floatValue

func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    //   if ( floatVersion >= 8.0) {

    let transitioningObject: TransitioningObject = TransitioningObject()
    transitioningObject.tabBarController = self
    return transitioningObject
    //}
}

Upvotes: 0

Views: 79

Answers (4)

sudhanshu-shishodia
sudhanshu-shishodia

Reputation: 1108

let iosVersion = (UIDevice.currentDevice().systemVersion as NSString).floatValue

func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

    switch iosVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch)
    {
        case .OrderedSame, .OrderedDescending:
            //iOS>=8.0.0
            let transitioningObject: TransitioningObject = TransitioningObject()
            transitioningObject.tabBarController = self
            return transitioningObject
        case .OrderedAscending:
            //iOS<8.0.0
            return nil
    }
}

Upvotes: 0

Zell B.
Zell B.

Reputation: 10296

Since your method return optional UIViewControllerAnimatedTransitioning you can return nil if version is lower then 8.0

func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
   if ( floatVersion >= 8.0) {

       let transitioningObject: TransitioningObject = TransitioningObject()
       transitioningObject.tabBarController = self
       return transitioningObject
    }else{
       return nil
    }
}

Upvotes: 2

WFT
WFT

Reputation: 277

The best way to do this, assuming your code may run on something below iOS 8 (which only seems reasonable, given your question) is to compare it to -[UIDevice systemVersion]. Check out the following Swift code:

switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
case .OrderedSame, .OrderedDescending:
    println("iOS >= 8.0")
case .OrderedAscending:
    println("iOS < 8.0")
}

The above was borrowed from NSHipster. There's a better way to do this if your code won't run on a device running below iOS 8 (probably only really useful once iOS 9 comes around). See here for further information.

Upvotes: 0

mc01
mc01

Reputation: 3780

Check this response for verifying system version.

You're on the right track already, just handle the if as you do for iOS >= 8.0, and else call an iOS7 alternative and return nil

let floatVersion = (UIDevice.currentDevice().systemVersion as NSString).floatValue

func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    if ( floatVersion >= 8.0) {

    let transitioningObject: TransitioningObject = TransitioningObject()
    transitioningObject.tabBarController = self
    return transitioningObject
    }else {
      //do something else here if it's iOS7 or earlier?
    }
    return nil
} 

Upvotes: 1

Related Questions