I've read many many articles, read many StackOverflow posts and tried different solutions, but I can't get it to work.
For an app that communicates with bluetooth headsets, the user must be able to start/stop a stopwatch when the user presses a button on the headset. When this button is pressed I receive a bluetooth event and start the stopwatch (receiving this bluetooth event in the background is not a problem). When the stopwatch is started I use the AVSpeechSynthesizer
to speak the text 'Stopwatch started'. Now comes my problem: I want to have a text to speech every minute, speaking the amount of minutes that elapsed. But also when the app is in the background...
I've read about solutions like this:
var bgTask = UIBackgroundTaskIdentifier()
bgTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
UIApplication.shared.endBackgroundTask(bgTask)
})
let timer = Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(notificationReceived), userInfo: nil, repeats: true)
RunLoop.current.add(timer, forMode: RunLoopMode.defaultRunLoopMode)
It did work, but I can't find anything about how stable this code is on production (when the app is downloaded from the App Store). Also scheduled local notifications can not trigger any code when the app is in the background. Even the property postUtteranceDelay
of AVSpeechUtterance
seems unstable. I set the property to 460 seconds, but the text was spoken after a random 56 seconds...
I turned on almost all the background modes for my app.
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>bluetooth-central</string>
<string>fetch</string>
<string>remote-notification</string>
</array>
Other people suggest to make a silent audio track. But it seems that apps using that kind of 'silent tracks' are being refused by the Apple Review team.
What depresses me the most, is that apps like Runkeeper and Seconds Interval Timer are able to do his. Even when I turn on flight mode and disable all settings (like push, motion sensing) from the apps. So, why can't I find a working solution...?
Does anyone have a suggestion where to start?