Ios 未调用CLLocationManager didUpdateLocations

Ios 未调用CLLocationManager didUpdateLocations,ios,swift,coordinates,cllocationmanager,Ios,Swift,Coordinates,Cllocationmanager,我正在学习使用Swift开发iOS 8应用程序。我已经跟随了一个关于如何在Swift和iOS 8中构建天气应用程序的教程 作为对应用程序的改进,作者/导师建议使用CLLocationManager获取设备的位置,以输入天气API,而不是硬编码的纬度和经度值 所以,在网上阅读了各种教程之后,我已经开始尝试实施这一改进建议 我已将负责获取位置坐标的代码放在AppDelegate.swift文件中 AppDelegate.swift代码 import UIKit import CoreLocation

我正在学习使用Swift开发iOS 8应用程序。我已经跟随了一个关于如何在Swift和iOS 8中构建天气应用程序的教程

作为对应用程序的改进,作者/导师建议使用
CLLocationManager
获取设备的位置,以输入天气API,而不是硬编码的纬度和经度值

所以,在网上阅读了各种教程之后,我已经开始尝试实施这一改进建议

我已将负责获取位置坐标的代码放在
AppDelegate.swift
文件中

AppDelegate.swift代码

import UIKit
import CoreLocation

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    var window: UIWindow?
    var locationManager: CLLocationManager!
    var errorOccured: Bool = false
    var foundLocation: Bool = false
    var locationStatus: NSString = "Not Started"
    var location: CLLocationCoordinate2D?
    var locationName: String?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        application.setStatusBarHidden(true, withAnimation: .None)
        initializeLocationManager()
        return true
    }

    func initializeLocationManager() {
        self.locationManager = CLLocationManager()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        println("didUpdateLocations running")
        if (foundLocation == false) {
            self.locationManager.stopUpdatingLocation()
            foundLocation = true
            var locationArray = locations as NSArray
            var locationObj = locationArray.lastObject as CLLocation
            var geoCoder = CLGeocoder()
            geoCoder.reverseGeocodeLocation(locationObj, completionHandler: { (placemarks, error) -> Void in
                var p = placemarks as NSArray
                var placemark: CLPlacemark? = p.lastObject as? CLPlacemark
                self.locationName = placemark?.name
            })
            self.location = locationObj.coordinate
        }
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        locationManager.stopUpdatingLocation()
        if ((error) != nil) {
            if (errorOccured == false) {
                errorOccured = true
                print(error)
            }
        }
    }

    // authorization status
    func locationManager(manager: CLLocationManager!,
        didChangeAuthorizationStatus status: CLAuthorizationStatus) {
            var shouldIAllow = false

            switch status {
            case CLAuthorizationStatus.Restricted:
                locationStatus = "Restricted Access to location"
            case CLAuthorizationStatus.Denied:
                locationStatus = "User denied access to location"
            case CLAuthorizationStatus.NotDetermined:
                locationStatus = "Status not determined"
            default:
                locationStatus = "Allowed to location Access"
                shouldIAllow = true
            }
            NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
            if (shouldIAllow == true) {
                NSLog("Location to Allowed")
                // Start location services
                locationManager.startUpdatingLocation()
            } else {
                NSLog("Denied access: \(locationStatus)")
            }
    }

}
func getCurrentWeatherData() -> Void {
    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    var forecastURL: NSURL
    var locName = "London"

    let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    appDelegate.foundLocation = false

    if let loc = appDelegate.location {
        println("Got Location!") // for debug purposes
        var currentLat = loc.latitude
        var currentLng = loc.longitude
        forecastURL = NSURL(string: "\(currentLat),\(currentLng)", relativeToURL: baseURL)
        locName = appDelegate.locationName!
    } else {
        println("No Location :(") // for debug purposes
        var currentLat = "51.513445"
        var currentLng = "-0.157828"
        forecastURL = NSURL(string: "\(currentLat),\(currentLng)", relativeToURL: baseURL)
    }

    let sharedSession = NSURLSession.sharedSession()

    let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
        var urlContents = NSString.stringWithContentsOfURL(location, encoding: NSUTF8StringEncoding, error: nil)
        if (error == nil) {
            let dataObject = NSData(contentsOfURL: location)
            let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary
            let currentWeather = Current(weatherDictionary: weatherDictionary)
            dispatch_async(dispatch_get_main_queue(), {
                () -> Void in
                self.locationNameLabel.text = "\(locName)"
                self.temperatureLabel.text = "\(currentWeather.temperature)"
                self.iconView.image = currentWeather.icon!
                self.currentTimeLabel.text = "At \(currentWeather.currentTime!) it is"
                self.humidityLabel.text = "\(currentWeather.humidity)"
                self.percipitationLabel.text = "\(currentWeather.percipProbability)"
                self.summaryLabel.text = "\(currentWeather.summary)"
                // Stop refresh animation
                self.refreshActivityIndicator.stopAnimating()
                self.refreshActivityIndicator.hidden = true
                self.refreshButton.hidden = false
            })
        } else {
            let networkIssueController = UIAlertController(title: "Error", message: "Unable to load data. Connectivity error!", preferredStyle: .Alert)
            let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
            networkIssueController.addAction(okButton)
            let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
            networkIssueController.addAction(cancelButton)
            self.presentViewController(networkIssueController, animated: true, completion: nil)

            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.refreshActivityIndicator.stopAnimating()
                self.refreshActivityIndicator.hidden = true
                self.refreshButton.hidden = false
            })
        }
    })

    downloadTask.resume()
}
然后在我的
ViewController.swift
文件中,我想获得位置坐标。代码如下:

ViewController.swift代码

import UIKit
import CoreLocation

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    var window: UIWindow?
    var locationManager: CLLocationManager!
    var errorOccured: Bool = false
    var foundLocation: Bool = false
    var locationStatus: NSString = "Not Started"
    var location: CLLocationCoordinate2D?
    var locationName: String?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        application.setStatusBarHidden(true, withAnimation: .None)
        initializeLocationManager()
        return true
    }

    func initializeLocationManager() {
        self.locationManager = CLLocationManager()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        println("didUpdateLocations running")
        if (foundLocation == false) {
            self.locationManager.stopUpdatingLocation()
            foundLocation = true
            var locationArray = locations as NSArray
            var locationObj = locationArray.lastObject as CLLocation
            var geoCoder = CLGeocoder()
            geoCoder.reverseGeocodeLocation(locationObj, completionHandler: { (placemarks, error) -> Void in
                var p = placemarks as NSArray
                var placemark: CLPlacemark? = p.lastObject as? CLPlacemark
                self.locationName = placemark?.name
            })
            self.location = locationObj.coordinate
        }
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        locationManager.stopUpdatingLocation()
        if ((error) != nil) {
            if (errorOccured == false) {
                errorOccured = true
                print(error)
            }
        }
    }

    // authorization status
    func locationManager(manager: CLLocationManager!,
        didChangeAuthorizationStatus status: CLAuthorizationStatus) {
            var shouldIAllow = false

            switch status {
            case CLAuthorizationStatus.Restricted:
                locationStatus = "Restricted Access to location"
            case CLAuthorizationStatus.Denied:
                locationStatus = "User denied access to location"
            case CLAuthorizationStatus.NotDetermined:
                locationStatus = "Status not determined"
            default:
                locationStatus = "Allowed to location Access"
                shouldIAllow = true
            }
            NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
            if (shouldIAllow == true) {
                NSLog("Location to Allowed")
                // Start location services
                locationManager.startUpdatingLocation()
            } else {
                NSLog("Denied access: \(locationStatus)")
            }
    }

}
func getCurrentWeatherData() -> Void {
    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    var forecastURL: NSURL
    var locName = "London"

    let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    appDelegate.foundLocation = false

    if let loc = appDelegate.location {
        println("Got Location!") // for debug purposes
        var currentLat = loc.latitude
        var currentLng = loc.longitude
        forecastURL = NSURL(string: "\(currentLat),\(currentLng)", relativeToURL: baseURL)
        locName = appDelegate.locationName!
    } else {
        println("No Location :(") // for debug purposes
        var currentLat = "51.513445"
        var currentLng = "-0.157828"
        forecastURL = NSURL(string: "\(currentLat),\(currentLng)", relativeToURL: baseURL)
    }

    let sharedSession = NSURLSession.sharedSession()

    let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
        var urlContents = NSString.stringWithContentsOfURL(location, encoding: NSUTF8StringEncoding, error: nil)
        if (error == nil) {
            let dataObject = NSData(contentsOfURL: location)
            let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary
            let currentWeather = Current(weatherDictionary: weatherDictionary)
            dispatch_async(dispatch_get_main_queue(), {
                () -> Void in
                self.locationNameLabel.text = "\(locName)"
                self.temperatureLabel.text = "\(currentWeather.temperature)"
                self.iconView.image = currentWeather.icon!
                self.currentTimeLabel.text = "At \(currentWeather.currentTime!) it is"
                self.humidityLabel.text = "\(currentWeather.humidity)"
                self.percipitationLabel.text = "\(currentWeather.percipProbability)"
                self.summaryLabel.text = "\(currentWeather.summary)"
                // Stop refresh animation
                self.refreshActivityIndicator.stopAnimating()
                self.refreshActivityIndicator.hidden = true
                self.refreshButton.hidden = false
            })
        } else {
            let networkIssueController = UIAlertController(title: "Error", message: "Unable to load data. Connectivity error!", preferredStyle: .Alert)
            let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
            networkIssueController.addAction(okButton)
            let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
            networkIssueController.addAction(cancelButton)
            self.presentViewController(networkIssueController, animated: true, completion: nil)

            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.refreshActivityIndicator.stopAnimating()
                self.refreshActivityIndicator.hidden = true
                self.refreshButton.hidden = false
            })
        }
    })

    downloadTask.resume()
}
上面的不工作。我的
didUpdateLocations
委托从未被调用。在调试控制台/输出中,我总是得到
无位置:(
打印出来,这表明获取位置失败,更具体地说,这表明我的
AppDelegate
上的Location属性是
nil

我为纠正这一点所做的事情:

  • 在info.plist中,我添加了两个键
    nslocationwhenUsageDescription
    NSLocationAlwaysUsageDescription
  • 确保我通过WiFi而不是以太网连接

  • 还有数不清的其他代码调整,但仍然一无所获。

    一些观察结果:

  • 正如您所指出的,如果要调用
    requestAlwaysAuthorization
    ,则必须设置
    NSLocationAlwaysUsageDescription
    。如果调用
    requestwhenuseauthorization
    ,则需要
    nslocationwhenuseagedescription
    。(您看到确认对话框的事实意味着您已经正确地执行了此操作。我假设您看到的是您在确认警报中提供的任何描述。)

  • 在模拟器上,您可能看不到设备上的位置更新。请在实际设备上进行测试

    当我使用你的代码时,我看到了
    didUpdateLocations
    ,当我从设备调用它时,但不是从模拟器调用它

  • 一旦解决了无法看到调用的
    didUpdateLocations
    的问题,就会出现另一个问题:

    您在授权状态更改时发布通知,但在异步接收位置时(即稍后)不会发布通知。坦率地说,从视图控制器的角度来看,后者是更关键的事件,因此我认为(a)您应该在接收位置时发布通知;以及(b)视图控制器应注意此通知。现在,即使您成功地获得要调用的
    didUpdateLocations
    ,视图控制器也不会收到此类通知

    此外,您的
    didUpdateLocations
    正在启动另一个异步过程,即坐标的地理编码。如果您的视图控制器也需要该过程,您应该在地理编码器的完成块内发布通知

    坦率地说,您甚至没有向我们展示视图控制器代码,该代码为该
    CLLocationManagerDelegate
    代码将调用的任何通知添加了一个观察者,但我假设您已经这样做了


  • 仅供记录:我首先放置了两个键(
    NSLocationAlwaysUsageDescription
    nslocationwhenusagedescription
    )进入
    测试plist
    而不是
    应用程序plist
    。我花了一些时间才意识到….

    对不起,我不知道Swift。但是LocationManager的一个常见错误是忘记保留它。您的var保留了吗?嗯,以前没有听说过,保留是什么意思?它只是文件顶部的一个
    var
    如你所见。它是否要求获得读取位置的权限?是的,在iOS模拟器中,它确实要求使用权限。很抱歉问一些琐碎的问题,但是:你在真实的设备上尝试过吗?你在模拟器中设置过位置吗?你尝试过重置模拟器吗?谢谢Rob,我认为第2点可能是这样的。我的想法正确吗我不需要成为一名苹果开发者(付费)才能在我的iPhone上进行测试吗?谢谢,我回家后会确保我没有这样做。