2017-03-18 30 views
1

即使位置管理器方法requestWhenInUseAuthorization()或startUpdatingLocation()已被調用,每當應用程序第一次運行時,位置管理器(_:確實更改授權:)是否被調用?我想在一個按鈕,我在下面的@IBAction調用的點擊報告位置:locationManager(_:確實更改授權:)在應用第一次運行時執行?

@IBAction func findOutPressed(_ sender: UIButton) { 
    getLocation() 
} 

我CoreLocation代碼是在下面的擴展:

extension ViewController: CLLocationManagerDelegate { 
     
    // Called from findOutPressed to get location when button is clicked 
    func getLocation() { 
        let status = CLLocationManager.authorizationStatus() 
        handleLocationAuthorizationStatus(status: status) 
    } 
     
    // Respond to the result of the location manager authorization status 
    func handleLocationAuthorizationStatus(status: CLAuthorizationStatus) { 
        switch status { 
        case .notDetermined: 
            locationManager.requestWhenInUseAuthorization() 
        case .authorizedWhenInUse, .authorizedAlways: 
            locationManager.startUpdatingLocation() 
        case .denied: 
            print("I'm sorry - I can't show location. User has not authorized it") 
        case .restricted: 
            print("Access denied - likely parental controls are restricting use in this app.") 
        } 
    } 
     
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
        handleLocationAuthorizationStatus(status: status) 
    } 
     
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
         
        let currentLocation = locations.last 
         
        let labelText = "My location is: latitude = \((currentLocation?.coordinate.latitude)!), longitude = \((currentLocation?.coordinate.longitude)!)" 
         
        resultLabel.text = labelText 
         
        locationManager.stopUpdatingLocation() 
    } 
     
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 
        print("Dang, there was an error! Error: \(error)") 
    } 
} 

我發現位置即使@IBAction findOutPressed沒有被點擊觸發,並且結果會在用戶點擊按鈕之前在我的最初空白resultsLabel中更新,但是Manager(更改授權:)正在立即執行。我知道我可以設置一個標誌來確定按鈕是否被點擊,從而防止標籤過早更新,但我試圖瞭解位置管理器(_:確實更改授權)何時被觸發。 Apple說: 「只要應用程序使用位置服務的能力發生變化,就會調用此方法。可能會發生更改,因爲用戶允許或拒絕爲應用程序或系統整體使用位置服務。」 這似乎並沒有涵蓋應用第一次運行時觸發的情況。我很感激任何能夠讓我直接理解授權更改發生的人。道歉,如果我錯過了一些基本的東西。 謝謝!

回答

3

didChangeAuthorization被調用時創建的CLLocationManager的時候,因此,即使這將觸發:

var locationManager = CLLocationManager() 
// ... 
locationManager?.delegate = self 
// ... 
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
    print("authorization checked...") 
} 

所以你可能要設置&在AppDelegate中的頂部初始化你的LocationManager?那會觸發它。然後會發生什麼是didChangeAuthorization開始更新位置,然後在實際點擊按鈕之前調用您的didUpudateLocation。

也許申報的LocationManager作爲一個可選的,僅在初始化findOutPressed()例如: -

// class var: 
var locationManager:CLLocationManager?location managers, 
// ... 
@IBAction func findOutPressed(_ sender: UIButton) { 
    locationManager = CLLocationManager() 
    locationManager?.delegate = self 

    getLocation() 
} 
+0

謝謝!這工作 - 非常感謝!我還注意到另一件奇怪的事情:如果我在模擬器中更改位置:例如從自定義位置到Apple,Apple位置不會隨後出現點擊而顯示。如果我退出模擬器並重新運行應用程序,我確實看到更改生效,但是如果我切換回自定義位置,則必須在看到更改之前執行相同的應用程序退出並重新運行。這是標準行爲還是其他奇怪的事情發生? – Gallaugher

相關問題