应用关闭后显示位置授权提醒

时间:2017-07-10 12:17:01

标签: ios cllocationmanager cllocation

我创建了一个单例类来处理位置授权,因为我在我的应用中需要几个视图。所以我创建了下面的Location.swift类。

注意:我已正确添加到Info.plist中,并查看了其他几个帖子但似乎没有解决此问题(至少没有找到)

protocol LocationServiceDelegate {
    func tracingLocation(currentLocation: CLLocation)
    func tracingLocationDidFailWithError(error: NSError)
}

class Location:  NSObject,CLLocationManagerDelegate {

    var latitude: Double!
    var longitude: Double!
    var currentLocation : CLLocation!

    var locationManager: CLLocationManager?
    var lastLocation: CLLocation?
    var delegate: LocationServiceDelegate?

    static let sharedInstance:Location = {
        let instance = Location()
        return instance
    }()

    override init() {
        super.init()
        self.locationManager = CLLocationManager()
        self.locationManager?.delegate = self

        guard let locationManagers = self.locationManager else {
            return
        }

        if CLLocationManager.authorizationStatus() == .notDetermined {
            locationManagers.requestWhenInUseAuthorization()
        }

        locationManagers.desiredAccuracy = kCLLocationAccuracyBest
        locationManagers.pausesLocationUpdatesAutomatically = false
        locationManagers.distanceFilter = 0.1

    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last else {
            return
        }
        self.lastLocation = location
        updateLocation(currentLocation: location)

    }

    @nonobjc func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        switch status {
        case .notDetermined:
            locationManager?.requestWhenInUseAuthorization()
            break
        case .authorizedWhenInUse:
            locationManager?.startUpdatingLocation()
            break
        case .authorizedAlways:
            locationManager?.startUpdatingLocation()
            break
        case .restricted:
            // restricted by e.g. parental controls. User can't enable Location Services
            break
        case .denied:
            // user denied your app access to Location Services, but can grant access from Settings.app
            break
        }
    }


    // Private function
    private func updateLocation(currentLocation: CLLocation){

        guard let delegate = self.delegate else {
            return
        }

        delegate.tracingLocation(currentLocation: currentLocation)
    }

    private func updateLocationDidFailWithError(error: NSError) {

        guard let delegate = self.delegate else {
            return
        }

        delegate.tracingLocationDidFailWithError(error: error)
    }

    func startUpdatingLocation() {
        print("Starting Location Updates")
        self.locationManager?.startUpdatingLocation()
        currentLocation = locationManager?.location
        Location.sharedInstance.latitude = currentLocation.coordinate.latitude
        Location.sharedInstance.longitude = currentLocation.coordinate.longitude
        print(Location.sharedInstance.latitude, Location.sharedInstance.longitude)
        //        self.locationManager?.startMonitoringSignificantLocationChanges()
    }

    func stopUpdatingLocation() {
        print("Stop Location Updates")
        self.locationManager?.stopUpdatingLocation()

    }


}

我的应用程序崩溃,我认为是因为位置授权未在开头设置。有趣的是,在您离开应用程序之前,提示用户允许位置服务的请求警报不会显示。

关闭应用并接受位置服务后,应用就可以了。所以我的问题是,为什么警报不会出现?

有趣的是,这只是通过实际设备发生的。在模拟器中,当加载初始视图时,警报会按预期弹出。

我的第一个应该加载和显示数据的视图如下:

import UIKit
import Alamofire

class CurrentWeatherVC: UIViewController {

    @IBOutlet weak var locationLabel: UILabel!
    @IBOutlet weak var weatherIcon: UIImageView!
    @IBOutlet weak var currentTempLabel: UILabel!
    @IBOutlet weak var weatherTypeLabel: UILabel!
    var currentWeather : CurrentWeather!

    override func viewDidLoad() {
        super.viewDidLoad()
        Location.sharedInstance.locationManager(manager: Location.sharedInstance.locationManager, didChangeAuthorizationStatus: .authorizedWhenInUse)
        currentWeather = CurrentWeather()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        Location.sharedInstance.startUpdatingLocation()
        currentWeather.downloadWeatherDetails {
                   self.updateMainUI()
        }
    }

    func updateMainUI() {
        //Double value convterted to string for current temp.
        //Added the degree symbol here
        //For forecast it gets added in before saved into list so be aware of that. 
        currentTempLabel.text = "\(currentWeather.currentTemp)°"
        weatherTypeLabel.text = currentWeather.weatherType
        locationLabel.text = currentWeather.cityName
        weatherIcon.image = UIImage(named: currentWeather.weatherType)
    }

}

1 个答案:

答案 0 :(得分:0)

我怀疑downloadWeatherDetails的实现使用了dataTask或在后台运行的其他NSURLSession方法之一。

确保仅在mainQueue

上调用UI内容
// ...
DispatchQueue.main.async {
    self.updateMainUI()
}
// ...