I am wondering if anyone knows of a method to notify a user, other than a notification window, when in background mode, and in particular in the “Executing a Finite-Length Task in the Background” mode. I have tried using haptics (vibration), changing color of the status bar, and flashing the screen (adjust brightness to make it flash). None of these work for me while in background mode. Perhaps I have a coding problem, but it is also likely that these are all disabled will in background mode.
Here is a snippet of the code…
func outputAccData(_ acceleration: CMAcceleration){
let angle = acceleration.y * 90 + 90
let angleString = String(format: "Angle: %.2f", angle)
Angle.text = angleString
if enabled == 1 {
switch UIApplication.shared.applicationState {
case .active:
if angle > sensitivity {
// vibrate if you exceed the sensitivity angle
// Trigger selection feedback.
feedbackGenerator?.impactOccurred()
feedbackGenerator?.prepare()
// change status bar to WHITE
UIApplication.shared.statusBarStyle = .lightContent
UIScreen.main.brightness = 0.1
timer = Timer
.scheduledTimer(timeInterval: interval,
target: self,
selector: #selector(changeBrightness),
userInfo: nil,
repeats: false)
} else {
Angle.backgroundColor = UIColor.clear
// change status bar to BLACK
UIApplication.shared.statusBarStyle = .default
}
case .background:
print("App is backgrounded.")
print("Background time remaining = \(UIApplication.shared.backgroundTimeRemaining) seconds")
if angle > sensitivity {
// vibrate if you exceed the sensitivity angle
// Trigger selection feedback.
feedbackGenerator?.impactOccurred()
// Keep the generator in a prepared state
feedbackGenerator?.prepare()
// change status bar to WHITE
UIApplication.shared.statusBarStyle = .lightContent
UIScreen.main.brightness = 0.1
timer = Timer
.scheduledTimer(timeInterval: interval,
target: self,
selector: #selector(changeBrightness),
userInfo: nil,
repeats: false)
} else {
// change status bar to BLACK
UIApplication.shared.statusBarStyle = .default
}
case .inactive:
break
}
}
}
func registerBackgroundTask() {
backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
self?.endBackgroundTask()
}
assert(backgroundTask != UIBackgroundTaskInvalid)
}
func endBackgroundTask() {
print("Background task ended.")
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid
}
func changeBrightness()
{
UIScreen.main.brightness = 1.0
}
}