Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
702 views
in Technique[技术] by (71.8m points)

iphone - How to support iOs 9 feature(s), while keeping iOs 8 support?

Basically the question is in the title. I'm sure there's a way to ship your app to an earlier version of iOs (8 in this case), while still being able to use an exclusive feature of a newer OS version. Example: My app has a minimum support of iOs 8.3, and with the latest release of iOs 9, I wanna add support to force touch (which is only available in iOs 9), but in the same time, I want to be able to fix bugs and do UI improvements for example and ship the update to both versions. Major apps do this, fb for example, so there MUST be a way, but I couldn't find it anywhere, I'm giving up :(

EDIT: is there a neat way to do it? without checking respondsToSelector and these kind of stuff? something like separate project? or maybe xcconfig files can do this trick? I'm just wondering.. because having an if else on every ios 9 targeted feature isn't really a good solution when you're talking about an enterprise/large app

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Swift 2's availability feature should solve your problems. With availability, Swift builds in support for OS versioning and symbol availability testing.

First, set your deployment target in build settings to iOS 8.3 and your base SDK to Latest. If you use symbols (classes, methods, etc) in your project that are only available in operating systems newer than your deployment target (iOS 8.3), Xcode will display an error message with a fix-it on that line when you try to build and run your project.

See Checking API Availability in the Control Flow chapter of The Swift Programming Language book.

An availability check looks like this:

if #available (iOS 9, *) {
    // use APIs only available on iOS 9 or later
} else {
    // do nothing, don't show feature in UI, etc
}

Those are the basics. In the second part of your question, what you're looking for is a way to handle API availability on a larger scale. To do this, you can use another feature of Swift 2's availability feature, the @available attribute. You can apply this attribute to any symbol definition–for example, a class–to mark that the definition of the symbol requires a certain minimum OS version.

Instead of using an availability check in every single place you use an iOS 9 API, you can just use the @available attribute on an entire class. Then you only need to use an availability check at the place you use that class. For example:

@available(iOS 9, *)
class MyAwesomeiOSNineFeature {
    var myCoolObject = UICooliOSNineThing()
    func doAwesomeStuff() {
        // lots of code that uses iOS 9 stuff, no availability check
    }

    func doOtherStuff() {
        // lots of code that uses iOS 9 stuff, no availability check
    }
}

// In another class that doesn't have an `@available` requirement:
if #available(iOS 9, *) {
    let feature = MyAwesomeiOSNineFeature()
    feature.doAwesomeStuff()
} else {
    // don't show UI for new feature
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...