如何使用Swift 4 iOS在特定时间间隔内更新用户的后台位置?

时间:2018-08-10 11:01:03

标签: ios swift timer core-location

我正在开发一个应用程序。我正在更新用户的背景位置。并且我想在特定时间间隔内更新该位置。我搜了很多。但没有任何解决方案。请帮帮我。下面是我的代码。预先感谢。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let userLocation :CLLocation = locations[0] as CLLocation
        print("user latitude = \(userLocation.coordinate.latitude)")
        print("user longitude = \(userLocation.coordinate.longitude)")

        self.labelLat.text = "\(userLocation.coordinate.latitude)"
        self.labelLongi.text = "\(userLocation.coordinate.longitude)"
        DataBaseFunctions.sharedInstance.saveUserDetail(dataToInsert: userLocation)
    }

1 个答案:

答案 0 :(得分:1)

这是Swift-NOW, 您可以将Timer用于didUpdateLocation与

locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.delegate = self
locationManager.startUpdatingLocation()
var timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateLocation), userInfo: nil, repeats: true)

func updateLocation() {
    let location: CLLocation = locationManager.location
    // Your code
}

//标记:-CLLocationManagerDelegate

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    // Your code
}