简体   繁体   English

每隔N分钟在后台迅速将位置发送到服务器

[英]Send the location to the server every n minutes in the background in swift

i want to sent the location to the server when the app is background, i tried with many way but can't get the solution 我想在应用程序为后台时将位置发送到服务器,我尝试了很多方法但无法获得解决方案

I tried with the timer function using the timer and using function 我尝试使用计时器功能和使用计时器功能

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

but i when i stop the timer when the app becomes active 但是我在应用程序激活时停止计时器

     if UIApplication.shared.applicationState == .active {
         RunLoop.current.cancelPerformSelectors(withTarget: self.bgtimer)
        timer.invalidate()

    }

but the app not function properly collection view and back function everything is not working properly 但该应用无法正常运行收集视图和后退功能无法正常运行

In DidBackground: 在DidBackground中:

  1. stop normalTimer. 停止normalTimer。
  2. start New backgroundTaskTimer 启动New backgroundTaskTimer

In WillForeground: 在WillForeground中:

  1. stop backgroundTaskTimer. 停止backgroundTaskTimer。
  2. start normalTimer 启动normalTimer

Find below sample code: 查找以下示例代码:

In Appdelegate: 在Appdelegate中:

Declaration: 宣言:

  var backgroundUpdateTask: UIBackgroundTaskIdentifier!

  var backgroundTaskTimer:Timer! = Timer()

Implementation: 执行:

func doBackgroundTask() {
    DispatchQueue.global(qos: .default).async {
        self.beginBackgroundTask()

        if self.backgroundTaskTimer != nil {
            self.backgroundTaskTimer.invalidate()
            self.backgroundTaskTimer = nil
        }

        //Making the app to run in background forever by calling the API
        self.backgroundTaskTimer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.startTracking), userInfo: nil, repeats: true)
        RunLoop.current.add(self.backgroundTaskTimer, forMode: RunLoopMode.defaultRunLoopMode)
        RunLoop.current.run()

        // End the background task.
        self.endBackgroundTask()

    }
}

func beginBackgroundTask() {
    self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(withName: "Track trip", expirationHandler: {
        self.endBackgroundTask()
    })
}

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

In ApplicationDidEnterBackground: 在ApplicationDidEnterBackground中:

 func applicationDidEnterBackground(_ application: UIApplication) {

    //Start the background fetch
     self.doBackgroundTask()
 }

In ApplicationWillEnterForeground: 在ApplicationWillEnterForeground中:

func applicationWillEnterForeground(_ application: UIApplication) {

    if self.backgroundTaskTimer != nil {
        self.backgroundTaskTimer.invalidate()
        self.backgroundTaskTimer = nil
    }
}

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

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