위치 추적 (CLLocationManagerDelegate) 및 내 vc (UIViewController)의 별도 클래스 관리를위한 클래스가 있습니다. VC 내 내 위치 클래스의 인스턴스를 시작하고 있습니다. 클래스가 시작되면 위치 인증이 요청되고 승인 또는 거부되면 다른 VC로 이동하여 앱에 대한 권한 요청 (알림)을 계속 진행하려고합니다.위치 액세스가 허용/거부 될 때까지 단절 일시 중지
지금은 요청 토스터가 팝업되고 Accept 또는 Deny가 선택되기 전에 UI가 배경의 다음 VC로 이어집니다.
내 위치 클래스의 delegate - 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)
}
}
}
}