CLLocationManager - 授权提示消失了吗?

问题描述 投票:0回答:1

我尝试构建一个locationHelper类来处理使用CLLocation的方法,这样我就不必在每个视图控制器中重写它们。我的LocationHelper类有一个方法checkStatus()来检查用户是否已授予auth使用他的位置。但是如果我调用该方法并且用户没有授予权限,则提示会出现,但在它到来后2秒就会消失。我认为问题来自于类的实现,因为如果我在ViewController中编写代码,则提示会一直持续到用户做出决定。我的LocationHelper.swift:

    import Foundation
import CoreLocation

class LocationHelper: NSObject, CLLocationManagerDelegate{
    var locationManager: CLLocationManager!
    var delegate: CLLocationManagerDelegate!
    override init(){
        super.init()
        self.locationManager = CLLocationManager()
        locationManager.delegate = self
    }

    func requestPermission() -> Void {
        self.locationManager?.requestWhenInUseAuthorization()
    }

    //func getLocationInstance() -> CLLocationManager {
    //    return self.locationManager?
    //}
    func checkStatus() -> Void {
        switch CLLocationManager.authorizationStatus() {
        case .notDetermined:
            // Request when-in-use authorization initially
            print("not determined")
            locationManager?.requestWhenInUseAuthorization()

        case .restricted, .denied:
            // Disable location features
            print("status: fail")

        case .authorizedWhenInUse:
            // Enable basic location features
            print("in use")


        case .authorizedAlways:
            print("always")
        }
    }

}

我的ViewController:

import UIKit

class LiveDataViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = UIColor(red: 0.12, green: 0.67, blue: 0.478, alpha: 1)
        let locHelp = LocationHelper()
        locHelp.checkStatus()


        //locHelp LocationHelper = LocationHelper()
        //locHelp.requestPermission()

    }

如果有人可以帮助我,我会很高兴。

ios swift class cllocationmanager
1个回答
2
投票

尝试保存对位置助手的引用。

视图加载后可能会自动释放。

像这样:

import UIKit

class LiveDataViewController: UIViewController {

    let locHelp = LocationHelper()

    override func viewDidLoad() {
        super.viewDidLoad()

        locHelp.checkStatus()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.