Call Location manager when app is in terminated state


When you want to create an app with location based you can use the monitor regions inside the location manager delegate.Monitor the location in the below method


            let region = self.region(withGeotification: CLLocationCoordinate2D(latitude: .latitude)!, longitude: (longitide)!), radius: CLLocationDistance((distance)!),identifier: idString, entry: true)
            locationManager.startMonitoring(for: region)


The below are the delegate methods for monitoring the regions.You can use it few methods in app delegate to monitor the locations


extension ViewController:CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    }
    
    func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
        print("Monitoring failed for region with identifier: \(region!.identifier)")
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Location Manager failed with the following error: \(error)")
    }

}



In App delegate you can use the below code

//    MARK: - Location handling with geo Fencing

    func handleEventForEnterRegion(forRegion region: CLRegion!) {
}

    func handleEventForExitRegion(forRegion region: CLRegion!) {
}


extension AppDelegate: CLLocationManagerDelegate {
    
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        if region is CLCircularRegion {
            handleEventForEnterRegion(forRegion: region)
            }
    }
    
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        if region is CLCircularRegion {
            if region is CLCircularRegion {
            handleEventForExitRegion(forRegion: region)
            }
        }
    }
}



Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift