简体   繁体   English

位置管理器无法快速更新位置。 AlertView消失了

[英]Location Manager not updating location in swift. AlertView disappears

I want to get location inside a custom delegate in swift. 我想快速获得自定义委托中的位置。 Note that this worked perfectly 2 hours ago. 请注意,这在2个小时前完美运行。 The major problem is that the Location authorization alertView disappears by its own before I get to Allow it. 主要的问题是,在获得允许之前,位置授权alertView会自行消失。 So i tried to go within settings and allow it but it does not work. 所以我试图进入设置并允许它,但是它不起作用。 Why is the alertView disappearing by it self and why even though I allowed it through the settings I still cannot get an update? 为什么alertView会自行消失,为什么即使我通过设置允许它仍然无法获得更新? I added the correct key in the plist and also added the delegate and CoreLocation framework in the file. 我在plist中添加了正确的密钥,还在文件中添加了委托和CoreLocation框架。 Also note that the didFail is not called at any point. 还要注意,在任何时候都不会调用didFail。 Any advise would be appreciated 任何建议将不胜感激

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)")
        }
}

Create a property from locationManager , because this way it is destroyed after you run your method. locationManager创建一个属性,因为这样,在您运行方法后,该属性将被销毁。 And don't forget to setup its delegate for example in viewDidLoad . 并且不要忘记在viewDidLoad设置其委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM