2016-12-18 59 views
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) 
     } 
    } 
} 

}

回答

0

不執行SEGUE的時候了。

請勿觸及您的MyLocation課程並直接向位置管理器發送消息。相反,向你的MyLocation類添加一個新的函數來請求授權,並讓該函數獲得一個完成塊。使完成塊取得一個狀態參數(請求授權,拒絕請求等)。

調用該函數,並執行performSegue在完成塊。

相關問題