为什么 locationManager didUpdateLocation 不工作?

Why isn't locationManager didUpdateLocation working?

将 CoreLocation.Foundation 添加到 BuildPhase 并在文件顶部导入后,如果我将以下内容放入带有按钮的视图控制器中,我可以获得位置信息:

@IBAction func locationButton(sender: AnyObject) {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
}

它使用 CLGeocoder().reverseGeocodeLocation completionHandler 继续到 locationManager didUpdateLocations,它在另一个函数中显示位置信息 - 这有效。

但是,当我尝试在我的数据模型中传输相同的代码时,它不起作用。我使用以下内容设置模型:

import CoreLocation

class Record: NSObject, CLLocationManagerDelegate
{
    let locationManager = CLLocationManager()

因为没有按钮,我把locationManager代码放到:

        override init()
{
    iD = NSUUID().UUIDString

    super.init()

    if (CLLocationManager.locationServicesEnabled())
    {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()

        switch CLLocationManager.authorizationStatus() {
        case .AuthorizedWhenInUse, .AuthorizedAlways:
            locationManager.startUpdatingLocation()
        case .NotDetermined:
            locationManager.requestWhenInUseAuthorization() // or request always if you need it
        case .Restricted, .Denied:
            print("tell users that they need to enable access in settings")
        default:
            break
        }
        print("Location services available")
        if CLLocationManager.authorizationStatus() == .NotDetermined
        {
            print("Still Not Determined")
        }
    } else { print("Location services not available") }

}

我得到 'Location services available'。但是下面的代码从不打印任何内容到控制台,也没有调用函数 toSetLocationStamped。

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
    print("started location man")
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler:        //pass the location co-ordinates
        {
            (placemarks, error) -> Void in

            if (error != nil)
            {
                println("Reverse geocoder failed with error" + error.localizedDescription)
                return
            }

            if placemarks.count > 0         //process the location array (placemarks)
            {
                let pm = placemarks[0] as! CLPlacemark
                self.toSetLocationStamped(pm)
                print("got here")
            } else
            {
                println("Problem receiving data from geocoder")
            }
    })
}

如果我放一个deinit class记录,用简单的打印日志,没有输出。

deinit
{
    print("deinit")
}

我正在初始化一个 dummyRecord:从 MasterViewController 中所需的初始化记录 class:

class MasterViewController: UITableViewController
{
var records = [Record]()
var subjectDescription: String?

// weak var delegate: MonsterSelectionDelegate?        // property for object conforming to MSDelegate

required init(coder aDecoder: NSCoder)      //      // coder because class is loaded from Storyboard
{
    super.init(coder: aDecoder)

    var dummyRecord1 = Record()
    dummyRecord1.details = "All was very good and strong with a little bit of lemon on the side of the hill."
    dummyRecord1.dateTimeEntered = NSDate(dateString: "2015-07-22")
    dummyRecord1.subject = "Clouds"
    dummyRecord1.locationEntered = "Drittelsgasse 1, 69493 Großsachsen, Germany."
    dummyRecord1.photos.append(UIImage(named: "zombies.jpg")!)

    records.append(dummyRecord1)
}

你打电话后

self.locationManager.requestWhenInUseAuthorization()

您无法立即开始更新位置。该调用是异步的。

以下是正确的做法:

1) 查看授权状态:

switch CLLocationManager.authorizationStatus() {
    case .AuthorizedWhenInUse, .AuthorisedAlways:
        locationManager.startUpdatingLocation()
    case .NotDetermined:
        locationManager.requestWhenInUseAuthorization() // or request always if you need it
    case .Restricted, .Denied:
        // tell users that they need to enable access in settings
    default:
        break
}

2) 如果您之前已授权您的应用程序,这应该会更新位置。但是,如果您不这样做,则会出现一个弹出窗口。为了响应授权状态的变化你需要添加另一个函数:

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if (status == .AuthorizedAlways) || (status == .AuthorizedWhenInUse) {
        locationManager.startUpdatingLocation()
    }
}