【问题标题】:CLLocationManagerDelegate not receiving updatesCLLocationManagerDelegate 未收到更新
【发布时间】:2016-02-06 16:10:46
【问题描述】:

我有一个具有这些功能的视图控制器:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

    print("loc")
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError){
    if(error.code == CLError.Denied.rawValue){
        print("error")
    }
}

是这个类的直接子类:

import UIKit
import CoreLocation

class UIViewLocationManager : UIViewController, CLLocationManagerDelegate{

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        print(status.rawValue)
        switch status {
        case .NotDetermined:
            break

        case .AuthorizedWhenInUse:
            if #available(iOS 9.0, *) {
                manager.requestLocation()
            } else {
                manager.startUpdatingLocation()
            }
            break

        case .AuthorizedAlways:
            break

        case .Denied:
            break

        default:
            break
        }
    }
}

然后我有这门课

class CoreLocationController : NSObject {

    func requestLocationUpdate(delegate : CLLocationManagerDelegate){
        let locationManager = CLLocationManager()
        locationManager.delegate = delegate
        if #available(iOS 8.0, *) {
            locationManager.requestWhenInUseAuthorization()
        } else {
            locationManager.startUpdatingLocation()
        }

然后在 AppDelegate 我声明它像:

let coreLocationController = CoreLocationController()

但是当我从 UIViewLocationManager 的子 viewController 调用 requestLocationUpdate(self) 时,我没有收到任何更新。但是,如果我只是将所有方法复制粘贴到CoreLocationController 并在CoreLocationController init() 方法中执行locationManager.delegate = self,那么一切正常。

有什么想法吗?我真的很绝望,因为我已经尝试了很多方法,但仍然无法正常工作。

提前致谢

【问题讨论】:

    标签: swift uiviewcontroller swift2 core-location


    【解决方案1】:

    locationManagerrequestLocationUpdate 方法中的一个局部变量。在对requestLocationUpdate 的调用结束时,locationManager 将被销毁,并且刚刚创建的 CLLocationManager 将没有任何引用它,因此它也将被销毁,尽管您已要求它发送消息到现存delegate

    如果您创建的CoreLocationController 的实例没有被破坏——某些东西总是指向该实例——那么将locationManager 更改为一个实例变量应该可以解决这个问题:

    class CoreLocationController : NSObject {
        var locationManager:CLLocationManager?
        func requestLocationUpdate(delegate : CLLocationManagerDelegate) {
            locationManager = CLLocationManager()
            locationManager?.delegate = delegate
            locationManager?.requestWhenInUseAuthorization()
        }
    }
    

    上面的代码在真实的 iPhone 5 和模拟的 iPhone 6 中都适用。位置更新委托代码被调用。我确实必须按照CLLocation Manager in Swift to get Location of User

    中的说明编辑 plist

    【讨论】:

    • 此解决方案部分有效。通过使用它,我在超类中的 didChangeAuthorizationStatus 被触发,但即使我在模拟器中设置了位置,我也没有从子类获得位置更新(“loc”从未打印)
    • 不确定,我从未在模拟器中使用过位置。试试真机?
    • 好吧,我不认为这是模拟器的问题。当我将所有代码都放在同一个类(CoreLocationController)中时,它就起作用了。我已经看到一些解决方案表明这可能与在主线程上分配委托有关。也许我明天会尝试一些关于线程的事情。无论如何,如果您有其他想法,请发布它
    • 您已编辑您的 plist? stackoverflow.com/questions/24050633/…
    • 当然可以。没有它,当我把委托给 CoreLocationController 时它甚至都不会工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多