简体   繁体   English

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

[英]How to update background location of user within specific time interval using swift 4 iOS?

I am working on an app. 我正在开发一个应用程序。 where I am updating a background location of a user. 我正在更新用户的背景位置。 and I want to update that location within a specific time interval. 并且我想在特定时间间隔内更新该位置。 I have searched a lot. 我搜了很多。 but did not get any solution. 但没有任何解决方案。 please help me out. 请帮帮我。 Here my code below. 下面是我的代码。 Thanks in advance. 提前致谢。

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)
    }

This is in Swift-NOW, You can use Timer for didUpdateLocation with 这是在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
}

// MARK: - CLLocationManagerDelegate //标记:-CLLocationManagerDelegate

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM