CLLocationManager的共享实例

时间:2015-10-10 21:56:39

标签: ios swift delegates location singleton

我正在尝试为我的应用程序创建简单的CLLocationManager共享实例。

创建共享实例没有问题:

import UIKit
import CoreLocation

protocol LocationHandlerDelegate: class {
    func locationHandlerDidUpdateLocation(location: CLLocation?)
    func locationHandlerDidFailWithError(error: NSError)
}

class LocationHandler: NSObject, CLLocationManagerDelegate {

    var locationManager: CLLocationManager!
    var location: CLLocation?
    weak var delegate: LocationHandlerDelegate?

    static let sharedInstance = LocationHandler()

    override init() {
        super.init()

        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyKilometer

        if CLLocationManager.authorizationStatus() == .NotDetermined {
            locationManager.requestWhenInUseAuthorization()
        }
    }

    // MARK: -
    // MARK: - CLLocationManagerDelegate functions
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        delegate?.locationHandlerDidUpdateLocation(locations.last)
        self.location = locations.last

        locationManager.stopUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        delegate?.locationHandlerDidFailWithError(error)

        locationManager.stopUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .AuthorizedAlways || status == .AuthorizedWhenInUse {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        }
    }
}

但出于某种原因,当我从另一个类调用我的共享实例时:

class TodayViewController: UIViewController, CLLocationManagerDelegate, LocationHandlerDelegate {
        super.viewDidLoad() {

        LocationHandler.sharedInstance.delegate = self        

        if #available(iOS 9.0, *) {
            LocationHandler.sharedInstance.locationManager.requestLocation()
        } else {
            LocationHandler.sharedInstance.locationManager.startUpdatingLocation()
        }
}
...
    // MARK: - LocationHandlerDelegate function
    func locationHandlerDidUpdateLocation(location: CLLocation?) {
        if let location = location {
            print("Current lcoation: \(location)")
        }
        else {
            // ...
        }
    }

    func locationHandlerDidFailWithError(error: NSError) {
        print("Error finding lcoation: \(error.localizedDescription)")
    }
}

我的LocationHandler中的CLLocationManagerDelegate函数都没有被调用。 但是如果在TodayViewController中我写了

LocationHandler.sharedInstance.locationManager.delegate = self

而不是

LocationHandler.sharedInstance.delegate = self

我在CLLocationManagerDelegate而不是TodayViewController中实现了LocationHandler个函数,然后调用了这些委托函数。所以我猜实例化可能存在一些问题?或者我错过了其他什么?

0 个答案:

没有答案