【问题标题】:Location Manager not updating location in swift. AlertView disappears位置管理器不会迅速更新位置。警报视图消失
【发布时间】:2015-02-16 13:11:45
【问题描述】:

我想快速获取自定义委托中的位置。请注意,这在 2 小时前运行良好。主要问题是位置授权警报视图在我允许它之前自行消失。所以我试图进入设置并允许它,但它不起作用。为什么 alertView 会自行消失,为什么即使我通过设置允许它仍然无法获得更新?我在 plist 中添加了正确的键,还在文件中添加了委托和 CoreLocation 框架。另请注意,在任何时候都不会调用 didFail。任何建议将不胜感激

func getLocation(){
    println("called")
    let locationManager:CLLocationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    let status = CLLocationManager.authorizationStatus()
    println(status.rawValue)

    if(status != CLAuthorizationStatus.Authorized) {
        locationManager.requestWhenInUseAuthorization()
        println("called2")
    }else{
        locationManager.startUpdatingLocation()
        println("allowed and updating")
    }




}



func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    println("updating")

    var locationArray = locations as NSArray
    var locationObj = locationArray.lastObject as CLLocation

    println(locationObj)

}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println(error)
}



func locationManager(manager: CLLocationManager!,
    didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        var shouldIAllow = false
        println(status)
        switch status {
        case CLAuthorizationStatus.Restricted:
            println("Restricted Access to location")
        case CLAuthorizationStatus.Denied:
            println("User denied access to location")
        case CLAuthorizationStatus.NotDetermined:
            println("Status not determined")
        default:
            println("Allowed to location Access")
            shouldIAllow = true
        }

        if (shouldIAllow == true) {
            manager.startUpdatingLocation()
        } else {
            println("Denied access: \(status)")
        }
}

【问题讨论】:

    标签: ios xcode swift core-location cllocationmanager


    【解决方案1】:

    locationManager 创建一个属性,因为这样在你运行你的方法后它就会被销毁。并且不要忘记在 viewDidLoad 中设置它的委托。

    【讨论】:

    • 好吧,我按照您的指示让它工作,但也将它从我的自定义委托中移开,这引发了我的问题:locationManager 更新位置是否需要在主线程上?我的意思是它通过自定义委托像 3 小时前一样完美地工作..
    • @snksnk 我不确定“将其从我的自定义委托中移开”是什么意思。自定义委托是什么意思?并回答你的问题:updateingLocations 在主线程上触发,所以它在主线程上。
    • 我创建了一个通过 mainViewController 调用的委托,我想通过该委托更新位置
    • 你的意思是 locationManager 的委托?
    猜你喜欢
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 2016-01-02
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多