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

走远了吗. 提交于 2019-11-29 15:10:24

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
}

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.

Fonix

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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!