简体   繁体   中英

CLLocationManager requestAlwaysAuthorization popup shows and hides immediately

CLLocationManager requestAlwaysAuthorization popup shows and hides immediately, leaving user no time to press allow button. This is a class I wrote for location managing:

import UIKit

class TMLocationManager: NSObject, CLLocationManagerDelegate {
    var locationManager: CLLocationManager?

    override init() {
        super.init()
    }

    static func startLocationManager() {
        let manager = TMLocationManager()
        manager.getPermission()
    }

    private func getPermission () {
        locationManager = CLLocationManager()
        locationManager!.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        locationManager!.delegate = TMLocationManager.init()

        switch CLLocationManager.authorizationStatus() {
        case .Denied, .Restricted:
            return
        case .NotDetermined:
            locationManager!.requestAlwaysAuthorization()
            break
        case .AuthorizedAlways, .AuthorizedWhenInUse:
            locationManager!.startUpdatingLocation()
        default:
            break
        }
    }

}

In my code I just write TMLocationManager.startLocationManager()

结合我的评论,一旦startLocationManager方法完成执行,您需要确保保留manager变量,因为它将被释放,并且警报已被startLocationManager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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