0
我有管理位置跟踪(CLLocationManagerDelegate)和一个单独的类我的VC(的UIViewController)一类。我正在VC中启动我的位置类的一个实例。当这个类被启动时,位置授权被请求,一旦被接受或拒绝,我想继续到另一个VC继续对应用(通知)的权限请求。直到位置授予访问权限暂停赛格瑞/拒绝
眼下,请求烤面包机弹出和UI塞格斯到下一个VC的背景前,接受或选择拒绝。
如何访问我的位置类的代表有什么建议 - 的LocationManager(_:didChangeAuthorization)FUNC从我的VC类,或者对如何做到这一点更好的想法?
这个想法不是让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)
}
}
}
}