iOS 中已弃用 9、兼容性?

deprecated in iOS 9, compatibility?

我已经用 Xcode 7 升级了我的 Xcode 项目。我正在使用 Objective-C。 当我在部署目标中定位 iOS 9.0 时,我收到 36 条警告:

  1. UIAlertView 已弃用,请使用 UIAlertController.
  2. ABAddressBookRef 已弃用,请使用 CNContactStore ...
  3. setStatusBarStyle:animated 已弃用,请使用 [UIViewController preferredStatusBarStyle]

和其他一些... 嗯,我想保持与 iOS 9 的兼容性,至少 iOS 8

我是否必须忽略这些警告以保持与不同 OS 的兼容性?

如果我使用 UIAlertController iOS 8,是否有效?

我必须做的最好的事情是什么?使用已弃用的代码或替换为新代码?

如果您不再针对旧版本的 iOS,那么建议您更新已弃用的代码。不过,您并非绝对必须这样做,Apple 的 SDK 仍正式支持已弃用的方法,但有一天 Apple 可能会删除这些方法。

以下是 Apple 对弃用的看法:

From time to time, Apple adds deprecation macros to APIs to indicate that those APIs should no longer be used in active development. When a deprecation occurs, it is not an immediate end-of-life to the specified API. Instead, it is the beginning of a grace period for transitioning off that API and onto newer and more modern replacements. Deprecated APIs typically remain present and usable in the system for a reasonable amount of time past the release in which they were deprecated. However, active development on them ceases and the APIs receive only minor changes—to accommodate security patches or to fix other critical bugs. Deprecated APIs may be removed entirely from a future version of the operating system.

As a developer, it is important that you avoid using deprecated APIs in your code as soon as possible. At a minimum, new code you write should never use deprecated APIs. And if you have existing code that uses deprecated APIs, update that code as soon as possible. Fortunately, the compiler generates warnings whenever it spots the use of a deprecated API in your code, and you can use those warnings to track down and remove all references to those APIs.

https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html

"Deprecated" 至少表示以下其中一项:

  • 这个 API 还没有消失,但它可能在未来,或者在未来 platforms/technologies。例如,在 iOS 8 之前弃用的所有 API 在 Swift 中不可用。
  • 有一个更好的替代方案 API,随着平台的变化,旧的替代方案可能无法继续满足您的所有需求。例如,AssetsLibrary 仍然存在,尽管它已被弃用,但它不提供对 iCloud 照片或实时照片的访问——对于那些你需要它替换照片框架的人。

当目标 OS 版本 API 被弃用时 需要考虑这些事情

但是,弃用警告基于您项目的最低部署目标。 如果您告诉 Xcode 您想要为 iOS 7 及更新版本构建,您不会看到从 iOS 8 或 iOS 9 开始弃用的 API 的警告。

如果您想部署回较旧的最低 OS 目标,但使用较新 OS 的功能,则需要在使用较新功能的代码中进行可用性检查。 Swift 见 Apple's docs on compatibility/availability and weak linking in general, and/or Checking API Availability