1 year ago
#151877
Rajat Dhasmana
Not getting callbacks for Geofence in DidEnter and DidExit methods. Getting error "error: Error Domain=kCLErrorDomain Code=0 "(null)"
class LocationManager: NSObject , ObservableObject {
static var shared = LocationManager()
private var locationManager = CLLocationManager()
@Published var currentLocation : CLLocationCoordinate2D?
@Published var currentRegion : MKCoordinateRegion = MKCoordinateRegion()
@Published var checkInStatus : CheckInOut = .checkOut
override init() {
super.init()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.requestLocation()
locationManager.startUpdatingLocation()
addGeofence()
if let status = UserDefaults.standard.string(forKey: "status") {
checkInStatus = CheckInOut(rawValue: status) ?? .checkOut
}
}
func addGeofence() {
let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: 28.5355, longitude: 77.3910), radius: 100, identifier: "Noida")
region.notifyOnEntry = true
region.notifyOnExit = true
if !CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
print("Geofencing is not supported on this device!")
return
}
locationManager.startMonitoring(for: region)
}
}
extension LocationManager : CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
LocationManager.shared.currentLocation = manager.location?.coordinate
if let loc = LocationManager.shared.currentLocation {
LocationManager.shared.currentRegion = MKCoordinateRegion(center: loc, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
guard let region = region else {
print("Monitoring failed for unknown region")
return
}
print("Monitoring failed for region with identifier: \(region.identifier)")
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
handleEvent(for: region, checkInStatus: .checkIn)
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
handleEvent(for: region, checkInStatus: .checkOut)
}
func handleEvent(for region: CLRegion , checkInStatus : CheckInOut) {
Api.hitDummyApi(successHandler: {
DispatchQueue.main.async {
UserDefaultsHelper.saveStatus(status: checkInStatus)
NotificationHandler.createLocalNotificationForCheckInStatus(region: region, checkInStatus: checkInStatus)
}
})
}
}
getting error everytime entering or exiting the region. For sometime the code was running fine but after some time it started showing error. I have checked "allow location simulation" in edit schemes section and added custom location as well. Also it is working if I change location using Xcode simulation but not if I change using iPhone simulator.
ios
swift
swiftui
core-location
geofencing
0 Answers
Your Answer