Display UIViewRepresentable in WidgetKit

Hi,

I'm trying to Display a MapView like the sytem Maps app does in a Widget.

I thought the best way would be to encapsulate the Mapview in a UIViewRepresentable like this:

Code Block
struct MapView: UIViewRepresentable {
  func makeUIView(context: UIViewRepresentableContext<MapView>) -> MKMapView {
    let mapView = MKMapView()
    return mapView
  }
  func updateUIView(_ view: MKMapView, context: UIViewRepresentableContext<MapView>) {
  }
}


And show it like this in my Widget:
Code Block
struct MapWidgetEntryView : View {
  var entry: Provider.Entry
  var body: some View {
    VStack {
      MapView()
    }
  }
}


Unfortunately this leads to this error:
Code Block
RemoteHumanReadableError: The operation couldn’t be completed. (BSServiceConnectionErrorDomain error 3.)
BSServiceConnectionErrorDomain (3):
==BSErrorCodeDescription: OperationFailed


I also tested this with other UIKit classes and it leads to the same error

Accepted Reply

Hello, our recommendation for displaying Maps content in widgets is to use a MKMapSnapshotter in your TimelineProvider methods. UIViewRepresentable is not supported in widgets.

Replies

Hello, our recommendation for displaying Maps content in widgets is to use a MKMapSnapshotter in your TimelineProvider methods. UIViewRepresentable is not supported in widgets.
I've tried MKMapSnapshotter in the TimelineProvider as suggested in the accepted answer, but the completion handler of the start is never called (snapshot is a MKMapSnapshotter object and I have verified that the callback is called when this code runs on the actual app and not the widget TimelineProvider. What am I missing?

Code Block      snapshot.start { (snapshot, error) in
      let image = snapshot?.image // Image(uiImage: snapshot?.image)
      completionHandler(image)
    }