3

The app is in the background and it receives a callback upon disconnection with the BLE device, after which the app has to wait for sometime(1minute) and then execute some piece of code. The app behaves as expected even when in the background if the screen is turned on. But if the screen is turned off then the timer is not working and the app is not executing as expected.

This is the code in AppDelegate to start a timer in background:

func startTimerWith(timeInterval: TimeInterval) {
    registerBackgroundTask()
    timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: false, block: { (timer) in
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Time"), object: nil)
        self.endBackgroundTask()
    })
}

func registerBackgroundTask() {
    backgroundTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
        self.endBackgroundTask()
    })
}

func endBackgroundTask() {
    print("Background task ended.")
    UIApplication.shared.endBackgroundTask(backgroundTask)
    backgroundTask = UIBackgroundTaskInvalid
    timer?.invalidate()
    timer = nil
}

When disconnected from the BLE device, I start the timer by registering to background task:

func disconnected(_ peripheral: CBPeripheral, with error: Error?) {
    print("DISCONNECTED!!!")
    AppDelegate.sharedApp().startTimerWith(timeInterval: TimeInterval(TIME))
    BLEDeviceHandler.sharedInstance.handleBLEDevice(connectedPeripheral!)
} 
Pritam
  • 339
  • 3
  • 23
yuvaraju
  • 167
  • 1
  • 10

2 Answers2

0

Two points are vital here :

  1. Timer doesn't work if the app is in background state for more than 10 minutes. I had an exact scenario where I had to perform some action in background. I found out that after 10 minutes, timer didn't work.
  2. Timers do not work when the device is locked. App gets suspended immediately once the device is locked. This is for iOS >= 7.0
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • 1. App in background, App has to start timer after disconnection from BLE device. After one minute of time app has to make api communication. This is working when app is in background but screen is not turn off(if screen is turn on, app works as expected).But here problem statement is, timer is not working when screen is turn off. 2. Timers are working fine in iOS 10.3 but not in 11.3 Plz help. – yuvaraju May 02 '18 at 09:13
0

Bug is fixed its because app using location services but I forgot to given permission to update location when app is in background.

yuvaraju
  • 167
  • 1
  • 10
  • Hi yuvaraju, what do you mean by "give permission to update location when app is in background". I have the same problem, the background task seems to be stopped when screen is off and my Timer don't fire in this case. In the app settings I have the "position" set at "Always" is that what you mean? I'm testing with iOS 12.1.4 – Andrea Gorrieri Apr 02 '19 at 13:08
  • Hi Andrea Gorrieri, sorry for the late reply, I am using the location service in my application, when the application goes to the background, location updates not happening. later I had given the below permission then my application has started working as expected. locationManager?.allowsBackgroundLocationUpdates = true locationManager?.pausesLocationUpdatesAutomatically = true – yuvaraju Apr 08 '19 at 09:53