nevinchanyi
nevinchanyi

Reputation: 27

Showing MKPolygon cause termination of app (iOS 18.0 public beta)

The code worked on previous iOS versions (16 and 17). Recently I updated an iPhone to iOS 18.0 public beta and now the code of presenting a route fails with fatal error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKPolygon needsElevationCorrection]: unrecognized selector sent to instance 0x303295cc0' *** First throw call stack: (0x18861f11c 0x18591e698 0x1887247d4 0x1885bc888 0x1885bc1b0 0x191b08f28 0x191b09150 0x1a86bd424 0x1a86be134 0x191a69344 0x1919d68b8 0x1919d4e90 0x1919d5078 0x1919d54b0 0x1919d59a8 0x1919cb31c 0x104655110 0x104497ca9 0x104498309 0x1043e555d 0x1043e9eb9 0x194084689) libc++abi: terminating due to uncaught exception of type NSException

The code that works on previous versions:

final class MapService: NSObject, MapServiceInterface, ObservableObject {
    
    var mapView = MKMapView()
    
    override init() {
        super.init()
        mapView.delegate = self
        setup()
    }

    func setRoute(coordinates: [CLLocationCoordinate2D]) async {
        let polyline = MKPolygon(coordinates: coordinates, count: coordinates.count)
        await mapView.addOverlay(polyline) // fails and terminates app here
        let edges = UIEdgeInsets(top: 10.0,
                                 left: 10.0,
                                 bottom: 12.0,
                                 right: 10.0)
        
        await mapView.setVisibleMapRect(
            polyline.boundingMapRect,
            edgePadding: edges,
            animated: true)
    }

    func mapView(_ mapView: MKMapView,
                 rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let renderer = MKPolylineRenderer(overlay: overlay)
        renderer.strokeColor = UIColor(.modernBlue300)
        renderer.lineWidth = 6
        return renderer
    }
}

The view:

struct MapView: View {
    @StateObject var viewModel = MapViewModel()
    var body: some View {
        Wrapper(view: viewModel.mapService.mapView)
   }
}

Crashlytics report

I tried to find information about this fatal error, not much information over the internet, the issue is new. Tried to change the thread for setting the route, and change the order of the code lines, give some time to present the MKMapView and then execute the adding polyline code.

Upvotes: -1

Views: 152

Answers (2)

Jacob White
Jacob White

Reputation: 91

You can stub out an extension to MKPolygon to make it work if you actually need a polygon and not just a polyline.

extension MKPolygon {
    @objc func needsElevationCorrection() -> Bool {
        return false
    }
}

Upvotes: 0

nevinchanyi
nevinchanyi

Reputation: 27

I found where the problem was. Instead of using

// works on iOS 16.2 - 17, but doesn't work on 18.0    
let polyline = MKPolygon(coordinates: coordinates, count: coordinates.count)

I tried

let polyline = MKPolyline(coordinates: coordinates, count: coordinates.count)

And it fixed the crash.

Somehow, it was enough for me to use MKPolygon to show route on previous iOS versions, but since iOS 18.0 public beta I have to use MKPolyline to show the route.

Upvotes: 0

Related Questions