简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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