每次寫新功能都要寫判斷 ios26以上才能用新功能 否則就要重寫一次適合舊版本的內容。有沒有什麼辦法可以讓我直接寫適用新版本的code,不能套用的話自動回退到舊版本

Asked on 06/12/2025

1 search

To address the challenge of writing code that works on iOS 26 and automatically falls back to older versions if necessary, you can leverage some of the new features and best practices introduced at WWDC 2025. Here are a few strategies:

  1. Use Conditional Compilation: Swift allows you to use conditional compilation to include or exclude code based on the platform or version. This can help you write code that only runs on iOS 26 or later.

  2. Adopt New APIs with Fallbacks: When using new APIs, you can provide fallback implementations for older versions. This ensures that your app remains functional even if certain features are not available.

  3. Runtime API Checks: Xcode 26 introduces more runtime API checks, which can help you identify and handle issues related to API availability. This can be configured in the Test Plan Configuration Editor. For more details, you can refer to the session What’s new in Xcode (30:15).

  4. Automatic Observation Tracking: UIKit now integrates Swift observation at its core, which can be back-deployed to iOS 18 by adding a specific key to your Info.plist. This allows you to use new observation features while maintaining compatibility with older versions. More information can be found in the session What’s new in UIKit (10:21).

By using these techniques, you can write code that takes advantage of new features in iOS 26 while ensuring compatibility with older versions.