Ios locationManager.stopUpdateLocations不';应用程序从后台状态返回后无法工作
我在后台使用地理位置跟踪,几分钟后,当应用程序在后台时,对locationManager.stopUpdateLocations()的调用停止工作-顶部的蓝色条没有删除,应用程序继续使用地理位置服务。如果调用locationManager.stopUpdateLocations()而不将应用程序最小化到后台,则服务将关闭,顶部的蓝色条将消失。任何遇到这种情况的人,请帮助,已经花了好几天时间——都没有用。禁用所有逻辑,剩下的就是:启动地理位置服务(locationManager.startUpdateLocations)并尝试停止(locationManager.stopUpdateLocations)。没有定时器或任何其他逻辑Ios locationManager.stopUpdateLocations不';应用程序从后台状态返回后无法工作,ios,swift,swift4,Ios,Swift,Swift4,我在后台使用地理位置跟踪,几分钟后,当应用程序在后台时,对locationManager.stopUpdateLocations()的调用停止工作-顶部的蓝色条没有删除,应用程序继续使用地理位置服务。如果调用locationManager.stopUpdateLocations()而不将应用程序最小化到后台,则服务将关闭,顶部的蓝色条将消失。任何遇到这种情况的人,请帮助,已经花了好几天时间——都没有用。禁用所有逻辑,剩下的就是:启动地理位置服务(locationManager.startUpda
import CoreLocation
final class MyLocationService: NSObject, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
func sendCoord() {
print("Send you coordinates")
}
func requestPermission() {
locationManager.requestWhenInUseAuthorization()
}
func start() {
locationManager.delegate = self
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.allowsBackgroundLocationUpdates = true
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 300
locationManager.startUpdatingLocation()
sendCoord()
}
func stop() {
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.stopUpdatingLocation()
locationManager.allowsBackgroundLocationUpdates = false
locationManager.delegate = nil
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
sendCoord()
}
}
这是我的ViewController类:
import UIKit
import Foundation
import CoreLocation
class ViewController: UIViewController {
// *** Internal variables *** //
private let locationService = MyLocationService()
var needPermissionForLocationServices = false
@IBOutlet weak var toolBar: UIToolbar!
@IBOutlet weak var startLocationToolBarButton: UIBarButtonItem!
@IBAction func startLocationToolBarButtonAction(_ sender: UIBarButtonItem) {
if CLLocationManager.locationServicesEnabled() {
switch(CLLocationManager.authorizationStatus()) {
case .notDetermined, .restricted, .denied:
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: "Error!", message: "Location services is off", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Go to settings?", style: UIAlertAction.Style.default, handler: { (alert: UIAlertAction!) in
print("")
UIApplication.shared.open(NSURL(string:UIApplication.openSettingsURLString)! as URL)
}))
self.present(alert, animated: true, completion: nil)
})
case .authorizedAlways, .authorizedWhenInUse:
locationService.start()
}
} else {
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: "Error!", message: "Location services is off", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Go to settings?", style: UIAlertAction.Style.default, handler: { (alert: UIAlertAction!) in
print("")
UIApplication.shared.open(NSURL(string:UIApplication.openSettingsURLString)! as URL)
}))
self.present(alert, animated: true, completion: nil)
})
locationService.requestPermission()
}
}
@IBOutlet weak var stopLocationToolBarButton: UIBarButtonItem!
@IBAction func stopLocationToolBarButtonAction(_ sender: UIBarButtonItem) {
locationService.stop()
}
// *** View Controller Functions *** //
deinit {
print("Deinit: VC NO MEMORY LEAKS")
}
override func viewDidLoad() {
super.viewDidLoad()
locationService.requestPermission()
}
override func viewDidDisappear(_ animated: Bool) {
}
override func viewWillAppear(_ animated: Bool) {
view.backgroundColor = UIColor.white
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
找到解决办法。您只需要为My Location服务类创建一个共享实例,而不是在ViewController中作为变量创建该类的实例,而是调用同一个单例。原因是我创建了我的位置服务类的不同实例,并在一个实例中启动位置,然后在另一个实例中尝试停止位置
class var sharedInstance: MyLocationService {
struct Singleton {
static let instance = MyLocationService()
}
return Singleton.instance
}
和调用类方法:
MyLocationService.sharedInstance.start()
请在上下文中显示代码。你的目标应该是制作一个实际的工作项目,我们可以用它来重现这个问题。我们不需要只是一些代码的摘录;我们需要查看代码在视图控制器中的确切位置,以及应该触发它的内容。好的,更改文本何时调用
stop
?当你的应用程序在后台时,调用stop
函数的代码可能没有执行。我通过单击viewController内的按钮来调用stop,因此该应用程序此时肯定处于活动状态,并且位于前台“原因是我创建了我的位置服务类的不同实例,在一个实例中启动了位置,并试图在另一个实例中停止它。“是的,这也是我的猜测。但这并不意味着Singleton是一个好的解决方案。还要注意的是,您的问题并没有暗示您是如何得到多个实例的。这是显而易见的,因为我每次启动ViewController时都创建了该类的一个实例