3

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?

Tarun Tyagi
  • 9,364
  • 2
  • 17
  • 30
Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • 1
    Upvoted. Commenting because I want to know the answer. My issue involves using something that became available starting iOS14 - and my apps require iOS 12. The only answer I had was to create *two* subclasses, which this may help me to resolve it using something **better** what you've found. –  Jun 19 '21 at 15:17

2 Answers2

2

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 {
    
}
Tarun Tyagi
  • 9,364
  • 2
  • 17
  • 30
  • 1
    Not sure if it will work (sounds like it should) but upvoted your answer like I just did the question. I'll give it a shot with my issue involving the new image picker introduced in iOS 14. Yes, a hack, but much better than littering your code with conditionals based on which iOS version the user is using. Hope it works for the OP! –  Jun 19 '21 at 15:20
  • Although a hack, it solves the general problem (+1). – Reinhard Männer Jun 20 '21 at 05:09
2

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

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • I don’t want to use deprecated code, but thanks for the hint that `MKMarkerAnnotationView` has been available earlier (+1). So the Xcode 13 warning „'MKPinAnnotationView' was deprecated in iOS 15.0: renamed to 'MKMarkerAnnotationView‘“ is misleading. But the general problem remains. – Reinhard Männer Jun 20 '21 at 04:48
  • @ReinhardMänner It isn't misleading. First A exists; then there is usually a period during which A and its prospective replacement B exists; then, sometimes after many years of coexistence, A is deprecated; and then finally, some time later even than that, A is removed. – matt Jun 20 '21 at 06:14
  • @matt I consider "renamed to ..." as misleading when it was not renamed but replaced. – Reinhard Männer Jun 20 '21 at 06:20
  • @ReinhardMänner Ah. Take it in two parts. Part 1: "It was deprecated." You: "Oh golly, what do I do now?" Part 2: "Fear not: there's a replacement name." So you see, they are helping you, not misleading you. – matt Jun 20 '21 at 06:23
  • "Renamed" is a poor choice of words. It should say "replaced by" or similar. – Paulw11 Jun 20 '21 at 07:29
  • On a side note, if you used .pinTintColor to change the color of an MKPinAnnotationView, you have to use .markerTintColor to change the color of an MKMarkerAnnotationView. – P. Stern Oct 01 '22 at 23:07