How to support iOs 9 feature(s), while keeping iOs

2019-01-28 11:54发布

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

3条回答
贼婆χ
2楼-- · 2019-01-28 12:20

set the "iOS Deployment Target" in Build Settings to 8.3 and your base SDK to Latest iOS (iOS 9.0).

you can have iOS 9 code in iOS 8 supported classes, just make sure any iOS 9 only code is never executed on a iOS 8 device. you can use respondsToSelector: or [[UIDevice currentDevice] systemVersion] to help.

查看更多
戒情不戒烟
3楼-- · 2019-01-28 12:22

a device being on iOS 9 doesnt mean it has 3d touch, you should use the forceTouchCapability property to determine if the device has 3d touch, and make certain parts of your code react appropriately

if you need to know which iOS version the device is running, take a look at this thread and you can use if-else statements to make your code react appropriately (like making sure the property actually exists on this devices OS version)

查看更多
beautiful°
4楼-- · 2019-01-28 12:25

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
}
查看更多
登录 后发表回答