2016-12-18 1 views
0

위치 추적 (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) 
     } 
    } 
} 

}

답변

0

은 바로 SEGUE을 수행하지 마십시오.

MyLocation 클래스에 손을 넣지 말고 메시지를 위치 관리자에게 직접 보내십시오. 대신 MyLocation 클래스에 새 함수를 추가하여 권한을 요청하고 해당 함수가 완료 블록을 가져 오도록하십시오. 완료 블록에 상태 매개 변수 (요청 승인, 요청 거부 등)를 가져 오십시오.

해당 기능을 호출하고 완료 블록에서 performSegue을 수행하십시오.

관련 문제