0

Maybe this a silly question but I genuinely can't find an answer to this anywhere. I want to let my iOS app send location updates to web server when it is in the background. Currently it doesn't work and I know the reason is because allowsBackgroundLocationUpdates is set to NO by default. My question is where can/should I set allowsBackgroundLocationUpdates to YES?

Edit: Let me add that I have already modified my Info.plist to include: Required background modes - App registers for location updates

B-Brennan
  • 111
  • 9
  • Possible duplicate of [Best way to use background location updates in iOS (Swift)](https://stackoverflow.com/questions/43182835/best-way-to-use-background-location-updates-in-ios-swift) – Adrian Feb 18 '18 at 17:40
  • Just set the property to `yes` when you instantiate your `CLLocationManager` – Paulw11 Feb 18 '18 at 17:44

1 Answers1

0

Hope i understood your question, First of all you need get the user's permission, call the requestAlwaysAuthorization() method

Code Sample from Apple:

let locationManager = CLLocationManager()   
func enableLocationServices() {
   locationManager.delegate = self

   switch CLLocationManager.authorizationStatus() {
      case .notDetermined:
         // Request when-in-use authorization initially
         locationManager.requestAlwaysAuthorization()
         break

      case .restricted, .denied:
         // Disable location features
         disableMyLocationBasedFeatures()
         break

      case .authorizedAlways:
         // Enable any of your app's location features
         enableMyAlwaysFeatures()
         break
      }
   }      
}

Note:- You are required to include the NSLocationWhenInUseUsageDescription and NSLocationAlwaysAndWhenInUseUsageDescription keys in your app's Info.plist file. (If your app supports iOS 10 and earlier, the NSLocationAlwaysUsageDescription key is also required.) If those keys are not present, authorization requests fail immediately.

Verify these steps:
1.set location manager's allowsBackgroundLocationUpdates to YES.
2.call the requestAlwaysAuthorization() method
3.Add NSLocationAlwaysUsageDescription in info.plist.
4.configure the app to run in the background(UIBackgroundModes in plist) maybe in sandbox.

Jeba Moses
  • 809
  • 8
  • 25
  • Thanks for the answer but I actually have location updating fine in normal use of my app. It's when I want to update it in the background that didUpdateLocations doesn't get called. – B-Brennan Feb 18 '18 at 19:01
  • I don't understand step 4 "configure...sandbox" –  Aug 31 '18 at 00:28