How to subclass a renamed class? / 'MKPinAnnotationView' was deprecated in iOS 15.0: renamed to 'MKMarkerAnnotationView‘

Viewed 274

MKPinAnnotationView was renamed in iOS 15 to MKMarkerAnnotationView.
Up to iOS 14, I subclassed MKPinAnnotationView as

class MyAnnotationView: MKPinAnnotationView {
// …
}  

The problem:
If my app is compiled to iOS 14, I want to declare MyAnnotationView as above.
However if it is compiled to iOS 15, I have to use

class MyAnnotationView: MKMarkerAnnotationView {
// …
}  

How can this be achieved?

On the instruction level, I could use something like

if #available(iOS 15, *) {
// iOS 15
} else {
// iOS 14
}  

But on the class level, apparently only something like

@available(iOS 15, *)
class MyAnnotationView: MKMarkerAnnotationView {   

seems to be available that lets me compile a class if iOS 15 is available, but apparently I cannot avoid compilation of class MyAnnotationView: MKPinAnnotationView { if iOS 15 is available.

So how is this handled in Swift?

2 Answers

As noted by @Paulw11 in other answer, MKMarkerAnnotationView has been available since iOS 11. So there's no need to be on iOS 15 to use it.


This part is still relevant to other cases like @dfd mentioned for image picker on iOS 14 and earlier versions.


Not as pretty as it should be, it's worth a shot anyway.

import MapKit

#if canImport(CoreLocationUI) // Hack for iOS 15?
public typealias BaseAnnotationView = MKMarkerAnnotationView
#else
public typealias BaseAnnotationView = MKPinAnnotationView
#endif

class MyAnnotationView: BaseAnnotationView {
    
}

MKPinAnnotationView hasn't been renamed.

MKPinAnnotationView is deprecated in iOS 15, but you can still use it. Deprecation just means it isn't recommended that you use it for new apps, it may go away in the future and you can't expect any enhancements.

MKMarkerAnnotationView has been available since iOS 11, so unless your app is targeting iOS 10 or earlier you can simply switch to using it instead of MKPinAnnotationView

Related