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)
}
}
}
}