【发布时间】:2016-07-17 18:41:27
【问题描述】:
我正在尝试使用名为 LocationService 的单例 locationManager 监控用户的重大位置变化,它是这样的:
LocationService.Swift:
class var sharedInstance: LocationService {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: LocationService? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = LocationService()
}
return Static.instance!
}
var locationManager: CLLocationManager?
var location: CLLocation?
var delegate: LocationServiceDelegate?
override init() {
super.init()
self.locationManager = CLLocationManager()
guard let locationManager = self.locationManager else {
return
}
if CLLocationManager.authorizationStatus() == .NotDetermined {
locationManager.requestAlwaysAuthorization()
}
locationManager.delegate = self
locationManager.distanceFilter = 10
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.pausesLocationUpdatesAutomatically = false
if NSString(string: UIDevice.currentDevice().systemName).floatValue >= 9 {
locationManager.allowsBackgroundLocationUpdates = true
}
}
func startUpdatingLocation() {
print("Starting Location Updates")
self.locationManager?.startUpdatingLocation()
}
func startMonitoringSignificantLocationChanges() {
print("Starting Significant Location Updates")
self.locationManager?.startMonitoringSignificantLocationChanges()
}
// CLLocationManagerDelegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else {
return
}
// singleton for get last location
self.location = location
}
myVC.swift:
private let locationService = LocationService.sharedInstance
func prepareInformation() {
self.locationService.delegate = self
self.locationService.startMonitoringSignificantLocationChanges()
}
但是 didUpdateLocations 在应用程序启动时只被调用一次,然后它甚至没有被调用。但是当我换行时:
self.locationService.startMonitoringSignificantLocationChanges()
到:
self.locationService.startUpdatingLocation()
效果很好,每次用户移动时都会调用。
可能是什么问题?谢谢!
【问题讨论】:
标签: ios iphone swift cocoa-touch core-location