即使用户退出视图并返回到上一个视图,我仍尝试进行位置更新。 为了做到这一点,我创建了一个独立的类来处理我的位置更新,但我遇到了奇怪的行为。位置更新无法正常工作
首先,我的授权请求只是弹出一瞬间,然后消失,所以我将其移回实际视图。 但是,我的位置更新仍然无法正常工作。 位置图标出现在设备上,但它永远不会进入didLocationUpdate功能。它也没有输入didFailWithError。 我很新的iOS,所以我不知道我做错了什么。
这里是我的类:
import CoreLocation
class RouteLocation: NSObject, CLLocationManagerDelegate{
var locationManager:CLLocationManager!
var databasePath = ""
override init(){
super.init()
let dirPaths =
NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
.UserDomainMask, true)
let docsDir = dirPaths[0] as NSString
databasePath = docsDir.stringByAppendingPathComponent(
"\\firstAscent.db")
// For use in foreground
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
// locationManager.requestAlwaysAuthorization()
// print("in location")
if CLLocationManager.locationServicesEnabled() {
print("System Services enabled")
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = 10
self.locationManager.startUpdatingLocation()
}
}
var isLocationMising = true
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
let latitute = locValue.latitude
let longitute = locValue.longitude
print("locations = \(locValue.latitude) \(locValue.longitude)") //continues to make SQL query calls
我把它从视图中使用:_ = RouteLocation()
http://stackoverflow.com/a/38961606/6297658 – Dravidian
我忘了提及这个确切的代码工作时,它是视图类的一部分。 – Xitcod13
你不会在你分配'RouteLocation'实例的地方显示代码,但是从你的描述中,你听起来像是在一个局部变量中分配它。只要该函数退出,局部变量就会释放,这意味着您的CLLocationManager实例被释放。您应该将您的'RouteLocation'对象分配为您的AppDelegate的属性,或者将其设置为一个单独的对象,以使其生命周期更长 – Paulw11