我目前正在創建一個使用CLLocationManager的應用程序,但不是在應用程序委託中提供詢問位置權限的提示,而是在按下「檢入」按鈕時執行該應用程序。我試圖創建一個閉包,允許用戶在他們接受允許定位服務後登記。到目前爲止,在用戶接受位置服務之後,檢查它們的代碼未被激活,因爲在用戶實際接受位置服務之前檢查位置服務是否已啓用。這裏是我的代碼:如何將閉包添加到系統方法?
typealias CompletionHandler = (success:Bool) -> Void
func askLocationPermission (completionHandler: CompletionHandler) {
self.locationsManager.requestWhenInUseAuthorization()
}
@IBAction func checkInButtonPressed(sender: AnyObject) {
askLocationPermission { (success) in
if CLLocationManager.locationServicesEnabled() {
self.locationsManager.delegate = self
if let location = self.locationsManager.location {
self.currentUserLatitude = location.coordinate.latitude
self.currentUserLongitude = location.coordinate.longitude
print("This is the current latitide: \(location.coordinate.latitude)")
print("This is the current longitude: \(location.coordinate.longitude)")
self.checkInLocation(
userInfo.sharedInstance.getAccessToken(),
id: userInfo.sharedInstance.getMemberID()!,
latitude: self.currentUserLatitude!,
radius: 0.3,
longitude: self.currentUserLongitude!)
}
}
}
}
它看起來不像completionHandler被稱爲 – PeejWeej
授權過程沒有完成處理程序。你將得到一個委託方法'didChangeAuthorizationStatus'的調用。可能最好的做法是在點擊簽入按鈕並請求位置訪問時設置一個標誌,然後在通過檢查代理方法中的標誌進行授權後恢復簽入過程。您還需要考慮一旦授予位置信息並開始獲取位置信息,則可能需要幾秒鐘才能獲得準確的修正 – Paulw11