Decoding the Payload
None of the code supplied by Xcode’s template will be necessary, so you should remove the entire contents of the NotificationViewController‘s implementation. Ignore the error saying that NotificationViewController does not conform to the UNNotificationContentExtension protocol. You’ll fix that in just a bit.
And later in section “Receiving the Notification” there is an instruction:
When a notification arrives, iOS will call the didReceive() method. Replace the contents of the method with the following:…
But there is nothing to replace if we previously deleted everything and are ignoring error dealing with conforming UNNotificationContentExtension protocol.
If we did click that error and added suggested stubs then we would again have gotten the didReceive()
method.
This is kinda obvious for everyone who is familiar with Swift and Xcode, but it is always pleasant to have instructions that are precise.
Receiving the Notification
// 1
decodeUserInfo(notification)
Should be
// 1
decodeUserInfo(notification: notification)
MKMapSnapshotter
As of Xcode 16.2 the proposed code runs without errors
snapshotter.start(with: .global(qos: .userInitiated)) { (snapshot, _) in
Implementation works well. But async implementation seems to work OK too:
@MainActor func didReceive(_ notification: UNNotification) {
// 1
decodeUserInfo(notification: notification)
// 2
// 1
var mapImage = Image(systemName: "globe.americas")
// 2
let group = DispatchGroup()
group.enter()
// 3
let options = MKMapSnapshotter.Options()
options.region = region
let snapshotter = MKMapSnapshotter(options: options)
Task {
// 4
let snapshot = try await snapshotter.start()
mapImage = Image(uiImage: snapshot.image)
let mapView = MapView(mapImage: mapImage)
mapViewHost = UIHostingController(rootView: mapView)
// 3
addChild(mapViewHost)
view.addSubview(mapViewHost.view)
// 4
mapViewHost.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
mapViewHost.view.topAnchor.constraint(equalTo: view.topAnchor),
mapViewHost.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
mapViewHost.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mapViewHost.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
// 5
mapViewHost.didMove(toParent: self)
}
}
1 Like