暂停segue直到授予/拒绝位置访问

时间:2016-12-18 16:34:33

标签: ios delegates swift3 cllocationmanager uistoryboardsegue

我有一个用于管理位置跟踪的类(CLLocationManagerDelegate)和一个用于我的vc(UIViewController)的单独类。我在VC中启动了我的位置类的实例。启动课程时,会请求位置授权,一旦接受或拒绝,我想转到另一个VC继续申请的许可请求(通知)。

现在,请求烤面包机弹出,并且在选择“接受”或“拒绝”之前,UI将在后台中切换到下一个VC。

有关如何从我的VC类访问我的位置类的委托 - locationManager(_:didChangeAuthorization)函数的任何建议,或者更好的想法如何实现这一点?

我们的想法是不要让VC类成为CLLocationManagerDelegate。'

我的位置分类:

class MyLocation: NSObject, CLLocationManagerDelegate {

    static let sharedManager = MyLocation()

    var locationManager = CLLocationManager()
    var allDelegates = NSHashTable<CLLocationManagerDelegate>.weakObjects()

    ....
    ....


    func performOnDelegates(_ aBlock:(CLLocationManagerDelegate) -> ()) {

        let hashTable = isObservingHighPrecisionLocationUpdates ? highPrecisionDelegates : allDelegates
        let locationManagerDelegates = hashTable.allObjects

        for aDelegate in locationManagerDelegates {
            aBlock(aDelegate)
        }
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {    

        print("LOCATION SERVICES: Re-evaluating state after authorization state changed")
        performOnDelegates { $0.locationManager?(manager, didChangeAuthorization: status) }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        performOnDelegates { $0.locationManager?(manager, didUpdateLocations: locations) }
    }

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

        print("WARNING: Location update failed with error = \(error)")
        performOnDelegates { $0.locationManager?(manager, didFailWithError: error) }
    }
}

我的视图控制器类:

import UIKit

class MyViewController: UIViewController, CLLocationManagerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func didTapNext(_ sender: UIButton) {
        if sender.restorationIdentifier == "locationAccessButton" {
        MyLocation.sharedManager.locationManager.requestWhenInUseAuthorization()
            performSegue(withIdentifier: "nextPermissionSegue", sender: nil)
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

不要立即执行segue。

请勿进入您的MyLocation课程并直接向位置管理员发送消息。相反,向MyLocation类添加一个新函数以请求授权,并使该函数采用完成块。让完成块获取状态参数(请求已授权,请求被拒绝等)。

调用该函数,并在完成块中执行performSegue

相关问题