Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2 API Availability Check for storyboard

Tags:

ios9

swift

swift2

I have an existing iOS application which is supports from iOS 8. i want to add new features only for iOS 9.

I have created a new storyboard for iOS 9 which is using the iOS 9 features like UIStackView. Appropriate storyboard file is instantiated based on the Device OS version.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let storyboard: UIStoryboard
    if #available(iOS 9, *, *) {
        storyboard = UIStoryboard(name: "Main", bundle: nil)
    } else {
        storyboard = UIStoryboard(name: "MainOld", bundle: nil)
    }
    let mainViewController = storyboard.instantiateInitialViewController()
    window?.rootViewController = mainViewController
    window?.makeKeyAndVisible()
    return true
}

But i am getting compilation error on Main.storyboard "UIStackView before iOS 9.0"

I am using Xcode 7 beta 5.

How to use the iOS 9 related features on the iOS 8 supported projects using Swift 2 #availability check

like image 454
jpsasi Avatar asked Aug 16 '15 16:08

jpsasi


2 Answers

I have got an answer from Quincey Morris on the Apple Cocoa developer email list.

By Default Build for setting in the File Inspector for storyboard file is "deployment Target", which is iOS 8, so compiler throws error.

If i change the "Build for" setting to iOS 9 on the storyboard which uses the iOS 9 features. NO Error, Application runs in both iOS 8 and iOS 9.

like image 195
jpsasi Avatar answered Sep 27 '22 19:09

jpsasi


You cannot use such #availability feature in StoryBoard. If you would like to support iOS 8 with UIStackView, you should construct UIStackView from code, or use third party's compatible libraries. Like OAStackView or TZStackView.

like image 35
kishikawa katsumi Avatar answered Sep 27 '22 19:09

kishikawa katsumi