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

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

最佳答案

这是在 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
}

关于ios - 如何使用 swift 4 iOS 在特定时间间隔内更新用户的后台位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51785053/

相关文章:

ios - fatal error : Index out of range when using UITableView with multiple sections

iphone - 如何从表中删除所有行?

iOS 11 导航 TitleView 错位

java - 如何在 JFrame 之间切换时保持 KeyListener 处于 Activity 状态

javascript - 如何用Javascript制作一个非常简单的倒计时器?

objective-c - 在哪里存储项目范围的配置变量?

ios - 用于存储日期的 CoreData 推荐做法

ios - 在 iOS 的 ViewController 中声明 View 时,为什么在 Swift 中使用 "weak"关键字

ios - 如何使 UIView 位于 UITabBarController 中所有选项卡的顶部

java - Spring JNDI 查找 EJB 计时器服务