我工作的一個應用程序,我希望存儲用戶的當前位置,當用戶註冊。如何等待didUpdateLocation方法調用其他方法之前叫什麼名字?
所以,一旦用戶點擊註冊按鈕locationManager.startUpdatingLocation()被調用,然後執行註冊操作。
然而,即使前didUpdateLocations委託方法返回一個座標的註冊方法被觸發,從而在用戶的位置被存儲在數據庫中的零。
試圖調用didUpdateLocations內的註冊的方法是,因爲亂七八糟,該委託被多次調用。
func signup() {
self.getLocation()
let point = PFGeoPoint(latitude:locationLat, longitude:locationLon)
...
newUser[PF_USER_LOCATION] = point
newUser.signUpInBackground(block: { (succeed, error) -> Void in
if let errorInSignup = error {
...
} else {
...
}
})
}
func getLocation() {
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
locationLat = locValue.latitude
locationLon = locValue.latitude
}
我只添加了部分代碼。
什麼是正確的程序要等到接收到的座標來調用註冊功能?
的requestWhenInUseAuthorisation()
是裏面的viewDidLoad()
更改呼叫方法肯定的作品 –
電話報名方式,只有當你收到當前位置,您可以使用關閉 – Diksha
如果y你很高興只能定位ios9和更高版本,你可以使用'requestLocation' https://developer.apple.com/reference/corelocation/cllocationmanager/1620548-requestlocation否則你需要在'didUpdateLocations'中編寫代碼來檢查適當的水平精度,然後停止位置更新並完成註冊。您還需要爲用戶拒絕位置權限或在合理時間內無法確定位置提供適當的代碼 – Paulw11