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
– Rici
@Felipericieri tested on an iPhone 5 and an iPhone 6, both with version 10.3
– João Victor
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– Leo Dabus
@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
– João Victor
"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
– Leo Dabus
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.
– Leo Dabus