ios - 位置管理器不会快速更新位置。 AlertView 消失

标签 ios xcode swift core-location cllocationmanager

我想快速获取自定义委托(delegate)内的位置。请注意,这在 2 小时前运行良好。主要问题是位置授权 alertView 在我允许之前自行消失。所以我试图进入设置并允许它,但它不起作用。为什么 alertView 会自行消失,为什么即使我通过设置允许它我仍然无法获得更新?我在 plist 中添加了正确的 key ,还在文件中添加了委托(delegate)和 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)")
        }
}

最佳答案

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

关于ios - 位置管理器不会快速更新位置。 AlertView 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28542193/

相关文章:

ios - 如何获取 XCode 应用程序的日志

ios - Swift - 使 UIViewController 可见,同时从 TOP 添加动画的新 ViewController(通过 self.present)

ios - 按下 tableView 单元格时在不同的 ViewController 中打开 URL

ios - 我怎样才能得到互联网服务提供商的名字?

ios - 使用ExtAudioFileWrite记录来自回调的数据

ios - CLLocationManager 在后台时不更新,并且得到困惑的位置坐标 Swift

xcode - 如何使用延迟位置 iOS 6?

ios - 为什么需要在这个人类中添加 2 个私有(private)变量?

ios - 快速和核心图形,捕捉点并撤消图像

arrays - Swift - 遍历嵌套的字典数组