1

I wanted to get user location updates when the app gets terminated.

What I tried: In AppDelegate:

var locationManger: CLLocationManager!

In the applicationDidFinishLaunching

setupLocationManager()

And also in AppDelegate:

@nonobjc func applicationWillTerminate(application: UIApplication) {
        locationManger.startMonitoringSignificantLocationChanges()
    }
func setupLocationManager(){
        locationManger = CLLocationManager()
        locationManger.delegate = self
        locationManger.requestAlwaysAuthorization()
        locationManger.desiredAccuracy = kCLLocationAccuracyHundredMeters
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if UIApplication.shared.applicationState == .inactive{
            let notification = UNMutableNotificationContent()
            notification.title = "Location Update"
            notification.body = "updated in background"

            let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
            let request = UNNotificationRequest(identifier: "locationUpdate", content: notification, trigger: notificationTrigger)

            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        }

    }

But i dont get a push notification... Any ideas? Referring to this post: IOS Getting location updates when app terminated without using significantChange it should be possible

Markus G.
  • 1,620
  • 2
  • 25
  • 49
  • have you turned on background location fetch? – vivek bhoraniya Sep 08 '17 at 07:48
  • Yes if you mean in Project Settings -> Capabilities -> Background Mode Location Updates – Markus G. Sep 08 '17 at 07:52
  • Possible duplicate of [How to Get Location Updates for iOS 7 and 8 Even when the App is Suspended](https://stackoverflow.com/questions/27742677/how-to-get-location-updates-for-ios-7-and-8-even-when-the-app-is-suspended) – Satish Babariya Sep 08 '17 at 07:53
  • @markus-g : As per the link you shared it looks like it is possible to get background location update if app is terminated. Please decline the accepted answer as it is wrong – Sandeep Bhandari Sep 08 '17 at 08:33

1 Answers1

0

Have you tried enabling background fetch here? enter image description here

prabodhprakash
  • 3,825
  • 24
  • 48