startMonitoringSignificantLocationChanges Does not work in background

Asked

Viewed 81 times

0

I’m making an app that uses the device’s location service. It necessarily needs to be capturing the positions when there is a change of location and should capture in all situations (background/foreground/Killed), so I’m using the method startMonitoringSignificantLocationChanges(), But when I send it to the background, the capture of positions only runs for a while longer and stops, after that no longer captures. I get around the distance that the documentation suggests to update (500m) position and nothing happens. What could I be doing wrong? Note: The simulator works perfectly, but the physical device does not work.

The archive Info.plist. is with background configuration enabled

<key>UIBackgroundModes</key>
  <array>
    <string>fetch</string>
    <string>location</string>
  </array>

Appdelegate.Swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if (launchOptions != nil) {
        let locationManager = CLLocationManager()
        locationManager.delegate = ViewController()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.activityType = CLActivityType.other
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.startUpdatingLocation()
    }

    return true
}

func applicationDidEnterBackground(_ application: UIApplication) {
    let locationManager = CLLocationManager()
    locationManager.delegate = ViewController()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.activityType = CLActivityType.other
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.startMonitoringSignificantLocationChanges()
}

Viewcontroller.Swift

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {    
    //Trato a captura da nova localização 
}
  • What device are you using? iOS model and version

  • @Felipericieri tested on an iPhone 5 and an iPhone 6, both with version 10.3

  • locationManager.delegate = Viewcontroller() is not correct locationManager.delegate = self. Viewcontroller() will generate an instance that is not the same as your app’s view controller

  • @Leodabus That code is inside the Appdelegate.Swift so I can use the callbacks that are in my Viewcontroller.Swift. In the simulator it works perfectly, but the physical device does not work

  • "so I can use the callbacks that are on my Viewcontroller.Swift" doesn’t make any sense. Do what I told you if you don’t understand edit your question

  • This code of yours in Appdelegate has no way to work. If you have something working, it’s not that. In addition to the Viewcontroller instance problem not being the same, Oce declared locationManager as a local variable within the method, when this applicationDidEnterBackground method is finished it will simply be discarded by Compiler. It simply ceases to exist.

Show 1 more comment

1 answer

-1

Two suggestions...

I noticed that, in the code sent, you are not asking for permission to pick up the user’s location... Maybe this is a problem. To resolve this, just put in viewDidLoad: locationManager.requestWhenInUseAuthorization()

Another possibility is that the documentation states that this function does not send repeated notifications in less than 5 minutes. That is, even if you move more than 500 meters, if not already 5 minutes past the last notification, the app will not notify you again until they pass these 5 minutes.

If you need to check the user’s location changes more often than that, I think you’ll have to use startUpdatingLocation() and calculate if the user has moved enough or not...

  • I request permission to pick up the user’s location at the time he enters the app, at Viewcontroller. The test I did was walking for 30 minutes, and during the walk I stopped for 10 minutes. Would that be enough? So I wanted the following behavior, when it’s open application I want to capture the location more often, so I use the startUpdatingLocation and when the application goes to background I want to use the startMonitoringSignificantLocationChanges(), so my code is on Appdelegate is it possible?

  • Sorry, just checking... did you ask permission only for when the app is in use or forever? How will you use the app in the background would have to be "always".

  • Another thing... Did you make the settings in the project to allow the background update? I never had to do anything that ran in the background, but searching I found the link tutorial at the end of this comment. And I don’t think the code needs to be in the appDelegate, do you need? https://www.raywenderlich.com/143128/background-modes-tutorial-getting-started

  • Yes, I request permission for the user "always". I also enabled capabilities for the app to run in the background. In the simulator it works perfectly, with all the behavior I need. But when I test with a real device, it does not work as expected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.