Necessity for backward compatibility:

The API of iOS and macOS is updated once a year, which brings a lot of useful new functions each time. However, many users do not upgrade to the latest version for various reasons. As a result, apps made with the API of the latest functions cannot be used on these iphones or Macs (if forced to use it, the program will flash back).

What if developers want to use the latest features to improve their App’s efficiency and experience without losing users who haven’t upgraded?

Solution:

1. Only a few new functions are used in the App:

You can use the API that comes with the new feature framework directly to determine whether it is supported on your current iPhone or Mac

2. Massive use of new functional frameworks in App:

If I want to make my App available to users with iOS12 or higher, I can change Xcode to ‘iOS12’ :

After compiling, Xcode automatically determines which apis are not compatible and provides several solutions:

Click the dot:

Two schemes are given:

1.#available– (ifelse, guard, etc.)

if #available(iOS 13, *) {// Use the new iOS13 API}else{// Use the old API}Copy the code

Principle:

#available(iOS 13, *) checks whether the current user’s iOS version is greater than or equal to iOS13, and returns true if it is greater than or equal to iOS13;

* All the new platforms that are likely to emerge in the future (e.g., Apple will launch the Apple Robot in 2030)

Ps: Guard is used like this:

guard #available(iOS 13, *) else { return }
Copy the code

2.@available– Global judgment, for the whole method or the whole class

@available(iOS 13, *)
func togglePeopleOcclusion() {// Use the new iOS13 API}Copy the code
@available(iOS 13.0, *) class ViewController: UIViewController {// use iOS13 new API}Copy the code

Same principle as above.


A mixture of the above two schemes:
@available(iOS 12, *)
func iOS12Work() {/ /...if #available(iOS 13, *) {
        iOS13Work()
    }
}

@available(iOS 13, *)
func iOS13Work() {/ /... }Copy the code
With the gradual integration of iOS and macOS, many oF the apis are becoming available in common, so you can also use them like this:
Available (iOS 13, macOS 10.15, *) funcnewMethod() {// use the new shared API in iOS13 and macOS10.15}Copy the code

The above

 

Little brother recently on the new iOS13 new tutorial, welcome to support:

M.cctalk.com/inst/s9vfhe…