UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Getting in and out of car

Forums > iOS

Does anyone know if the Reminders App functionality to reming when "getting in and out of the car" can be resembled via API? Is that CoreBluetooth or something else?

3      

I found the trick!

It's neither CoreBluetooth nor CarPlay. It's in AVAudioSession! The AVAudioSession has a routeChangedNotification. So as long as you hold a AVAudioSession you can register for this event:

let nc = NotificationCenter.default
        nc.addObserver(self, selector: #selector(handleRouteChanged), name: AVAudioSession.routeChangeNotification, object: nil)
@objc func handleRouteChanged(notification: Notification) {
        os_log("Route changed.", log: OSLog.App, type: .info)
        session.currentRoute.outputs.forEach { output in
            os_log("Output: %@", log: OSLog.App, type: .info, String(describing: output))
            os_log("Port: %@", log: OSLog.App, type: .info, String(describing: output.portName))
            os_log("Port Type: %@", log: OSLog.App, type: .info, String(describing: output.portType))
        }
    }

3      

This sounds weird to be honest. Are you sure this will work as expected? This should be notification for when user changes any audio output.

3      

Yes sure! That's kind of a misuse. But it's a trick that you can certanly use. You could start location tracking and motion processing to check if you're really tacking a trip with your car. Holding an AVAudioSession is much cheaper on energy that the other two. So it's just a trick to know when to spend more energy.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.