简体   繁体   English

后台位置每n分钟更新一次

[英]Background location update every n minutes

I'm writing an application that requires location updates every n minutes and sends data to a server even when the application is in background mode. 我正在编写一个需要每n分钟更新一次位置的应用程序,即使该应用程序处于后台模式,它也会将数据发送到服务器。 I have gone through so many links about this task. 我已经完成了很多有关此任务的链接。 As I found the proper solution is to use a timer and make it as a background task. 我发现正确的解决方案是使用计时器并将其作为后台任务。

Concerning to this I have two questions: 对此,我有两个问题:

  1. How can I implement these regular background location updates? 如何实施这些常规的后台位置更新? I understand that Apple allows background tasks for an only certain time. 我了解到Apple只允许在一定时间内执行后台任务。 So how can I make long-running background timer? 那么我该如何使长时间运行的后台计时器呢?
  2. Will apple reject the application with such kind of logics? 苹果会拒绝采用这种逻辑的应用程序吗?

inside appdelegate create method like this appdelegate内部的创建方法是这样的

      func getLocation()
      {
              locationManager.allowsBackgroundLocationUpdates = true
              locationManager.delegate = self
              locationManager.desiredAccuracy = kCLLocationAccuracyBest             
              locationManager.startUpdatingLocation()
      }

inside didFinishLaunchingWithOptions method in appdelegate 在appdelegate中的didFinishLaunchingWithOptions方法中

var n = 10 //seconds

var timer = Timer.scheduledTimer(timeInterval: n, target: self, selector: #selector(getLocation), userInfo: nil, repeats: true)

set the delegate method for cllocationmanager 设置cllocationmanager的委托方法

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
  {
    locationManager.stopUpdatingLocation()
    locationManager.delegate = nil
    //get the coordinates or do some code
  }

Use following code.This will work for 3 minutes 使用下面的代码,它将持续3分钟

Call this didenterbackground or foreground 将此称为didenterbackground或前台

var time = 180
func applicationDidEnterBackground(_ application: UIApplication)
    {

        self.doBackgroundTask()
    }

 var timerBack  = Timer()
    func doBackgroundTask() {

        DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
            self.beginBackgroundUpdateTask()

              print("Background time remaining = \(UIApplication.shared.backgroundTimeRemaining) seconds")

            self.timerBack = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(AppDelegate.displayAlert), userInfo: nil, repeats: true)
            RunLoop.current.add(self.timerBack, forMode: RunLoopMode.defaultRunLoopMode)
            RunLoop.current.run()

            self.endBackgroundUpdateTask()
        }
    }
    var backgroundUpdateTask: UIBackgroundTaskIdentifier!

    func beginBackgroundUpdateTask() {
        self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
            self.endBackgroundUpdateTask()
        })
    }

    func endBackgroundUpdateTask() {
        UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
        self.backgroundUpdateTask = UIBackgroundTaskInvalid
    }

//Do whatever you want //做你想做的

func displayAlert{


}

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

相关问题 如何在我的 iOS 应用程序中每 n 分钟更新一次后台位置? - How do I get a background location update every n minutes in my iOS application? 如何在iOS应用中每n分钟获取一次后台位置更新? - How do I get a background location update every n minutes in iOS app? 每隔N分钟在后台迅速将位置发送到服务器 - Send the location to the server every n minutes in the background in swift 应用程序进入后台后每n分钟获取用户位置 - Getting user location every n minutes after app goes to background 3分钟后iOS 9位置更新后台任务 - iOS 9 Location Update Background task after 3 minutes 安排在iOS应用中永远每隔N分钟运行一次后台任务 - Schedule background task running every n minutes for ever in iOS apps 如何创建每隔n分钟无限次调用的后台函数 - How to create a background function that is called every n minutes indefinately 每隔5分钟Phonegap在后台唤醒应用程序以检查iOS位置 - Phonegap every 5 minutes wake app in background to check location iOS Xamarin/MAUI iOS 每 15 分钟在后台更新应用程序的最佳方法? - Xamarin/MAUI iOS best approach to update app in background every 15 minutes? 每n分钟在标签中显示NSString - Displaying NSString in a label every n minutes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM