2016-12-18 3 views
0

私は位置追跡(CLLocationManagerDelegate)と私のvc(UIViewController)のための別のクラスを管理するためのクラスを持っています。私は、VC内の自分のロケーションクラスのインスタンスを開始しています。クラスが開始されると、位置認可が要求され、それが受け入れられたり拒否されたりすると、別のVCにセグをして、アプリケーションの許可要求を続ける(通知)必要があります。ロケーションアクセスが許可/拒否されるまでセグを一時停止

今すぐ、要求トースターがポップアップし、AcceptまたはDenyが選択される前にUIがバックグラウンドで次のVCにセグメンテーションされます。

VCクラスからmy locationクラスのdelegate - locationManager(_:didChangeAuthorization)funcにアクセスする方法や、これを達成するための良いアイデアがありますか?

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

はすぐセグエを行わないでください。

MyLocationクラスに手を届かずにメッセージをロケーションマネージャに直接送信してください。代わりに、MyLocationクラスに新しい関数を追加して承認を要求し、その関数を完了ブロックにします。完了ブロックにステータスパラメータ(許可要求、拒否要求など)を取らせます。

この関数を呼び出して、完了ブロック内のperformSegueを実行します。

関連する問題