I am trying to implement location-triggered-notification in an iOS app, which I recently started using SwiftUI.
As a first experimenting step, I have a button. And when tapped it should create a notification intented to fire once I get close to a certain location. As a starting point I read this page. But it is not all crystal clear when putting it into practice.
The location part is working but not the notification one. Though I have used CoreLocation and UserNotifications before, I have no previous experience with location triggered notifications.
Hereafter is the relevant (not working) code that I have at this point. Any tip or advice, in the right direction to make it work will be welcome.
import SwiftUI
import MapKit
struct ContentView: View {
......
func handleShareLocation() {
print(#function)
//TRIAL-CODE
guard CLLocationManager.locationServicesEnabled() else {
return
}
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert, .sound, .badge],
completionHandler: { _,_ in //[weak self] granted, _ in
/*guard granted else {
self?.delegate?.notificationPermissionDenied()
return
}*/
let notificationInfo = LocationNotificationInfo (notificationId: "nyc_promenade_notification_id",
locationId: "nyc_promenade_location_id",
radius: 500.0,
latitude: 40.696503,
longitude: -73.997809,
title: "Welcome to the Brooklyn Promenade!",
body: "Tap to see more information",
data: ["location": "NYC Brooklyn Promenade"])
self?.requestNotification(notificationInfo: notificationInfo)
})
//TRIAL-CODE
}
var body: some View {
VStack {
Spacer()
Button(action: {
self.handleShareLocation()
}) {
Text("Create location notification")
.padding()
}
Spacer()
}
}
}
Beside, I am also using the class:
class LocationNotificationScheduler: NSObject {
........
}
coming from the document I mentioned. In there two unclear points are:
- Where is LocationNotificationSchedulerDelegate coming from?
- What is notificationScheduled?
Those two points are causing error messages like:
Use of undeclared type 'LocationNotificationSchedulerDelegate'
Let me know if more information is needed.