如何支持iOs 9功能,同时保持iOs 8的支持? [英] How to support iOs 9 feature(s), while keeping iOs 8 support?

查看:115
本文介绍了如何支持iOs 9功能,同时保持iOs 8的支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上问题在于标题。我确信有一种方法可以将您的应用程序发送到早期版本的iO(在本例中为8),同时仍然可以使用较新操作系统版本的独有功能。
示例:
我的应用程序对iOs 8.3的最低支持,并且对于最新版本的iOs 9,我想添加对力触摸的支持(仅在iOs 9中可用),但同样时间,我希望能够修复错误并进行UI改进,并将更新发送到两个版本。
主要应用程序执行此操作,例如fb,所以必须有一种方法,但我找不到它,我放弃了:(

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 :(

编辑:有没有整洁的方式吗?没有检查 respondsToSelector 和这些东西?类似的东西单独的项目?或者也许xcconfig文件可以做到这一点?我只是想知道..因为在每个ios 9目标功能上有一个 if else 并不是一个好的解决方案当你谈论企业/大型应用程序时

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

推荐答案

Swift 2的可用性功能可以解决你的问题。有了可用性,Swift构建支持操作系统版本控制和符号可用性测试。

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

首先,将构建设置中的部署目标设置为iOS 8.3,将基础SDK设置为最新。如果使用符号(类)项目中的方法等,仅在比部署目标(iOS 8.3)Xcode更新的操作系统中可用当您尝试构建和运行项目时,将在该行显示一条错误消息。

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.

请参阅控件中的apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID523>检查API可用性 Swift编程语言书的流程章节。

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

可用性检查如下所示:

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

这些是基础知识。在您的问题的第二部分,您正在寻找的是一种更大规模处理API可用性的方法。为此,您可以使用Swift 2可用性功能的另一个功能, @available 属性。您可以将此属性应用于任何符号定义(例如,类),以标记符号的定义需要某个最小操作系统版本。

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.

您可以使用 @available 整个类的属性。然后,您只需要在使用该类的位置使用可用性检查。例如:

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
}

这篇关于如何支持iOs 9功能,同时保持iOs 8的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆