HomeiOS Developmentswift - Background Location Monitoring iOS

swift – Background Location Monitoring iOS


I want some assist in monitoring person location in background, suspended and killed state.

Downside Assertion

I wish to monitor person location after his consent in my App. I wish to monitor person location in all of the states Background, Suspended and Foreground. Until person has stopped it from the app. I’m not in a position to monitor location in background and killed state however foreground is working nice.

I’ve additionally enabled beneath settings in my locationManager

locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.showsBackgroundLocationIndicator = true


locationManager.startMonitoringSignificantLocationChanges()

What I’ve tried until now’s

  • Enabled background modes
  • Location updates (checked)
  • Background fetch (checked)
  • Background processing (checked)
  • Permission to Permit At all times location entry

Created a singleton for location monitoring examine the beneath code and calling this class on
didFinishLaunchingWithOptions – AppDelegate

   class EmployeeAttendanceTracker: NSObject,CLLocationManagerDelegate {


   static let shared = EmployeeAttendanceTracker()

personal let locationManager = CLLocationManager()
personal var lastLocationDate = Date()
static let LOCATION_INTERVAL = 1

var locationUpdate: (() -> Void)?

personal override init() {
    tremendous.init()
    setupLocationManager()
}

personal func setupLocationManager() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.activityType = .different
    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.pausesLocationUpdatesAutomatically = false
    locationManager.showsBackgroundLocationIndicator = true
    
    
    if #obtainable(iOS 9.0, *) {
      locationManager.requestAlwaysAuthorization()
    } else {
      locationManager.requestWhenInUseAuthorization()
    }
}
  //    
//
//    // MARK: - CLLocationManagerDelegate
//    
    func locationManager(_ supervisor: CLLocationManager, didChangeAuthorization      standing: CLAuthorizationStatus) {
        change standing {
        case .restricted:
            //showLocationPermissionAlert()
            Logger.s("Location entry restricted.")
        case .denied:
            //showLocationPermissionAlert()
            Logger.s("Consumer denied location entry.")
        case .notDetermined:
           // showLocationPermissionAlert()
            Logger.s("Location entry not decided.")
        case .authorizedAlways:
            if #obtainable(iOS 9, *) {
                locationManager.requestLocation()
            } else {
                locationManager.startUpdatingLocation()
                locationManager.startMonitoringSignificantLocationChanges()
            }
        default:
           // showLocationPermissionAlert()
            break
        }
    }


func locationManager(_ supervisor: CLLocationManager, didUpdateLocations places: [CLLocation]) {
guard let location = places.final else { return }

Logger.s("Consumer latitude: (location.coordinate.latitude), longitude: (location.coordinate.longitude)")
locationManager.stopUpdatingLocation()

let now = Date()
if isItTime(now: now as NSDate) {
    if shouldSendLocationToServer() {
        self.sendLocationToServer(location: location,completion: {
            self.locationUpdate?()
        })
    }else{
        self.locationUpdate?()
    }
    Logger.s(now)
    Logger.s(location)
}else{
    self.locationUpdate?()
}

  }

Any assistance on this may be tremendously appreciated

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments