Skip to content

Instantly share code, notes, and snippets.

@tetek
Last active October 26, 2022 05:05
Show Gist options
  • Select an option

  • Save tetek/b01ac58710f945dd015f9006c8064420 to your computer and use it in GitHub Desktop.

Select an option

Save tetek/b01ac58710f945dd015f9006c8064420 to your computer and use it in GitHub Desktop.
// Authorize the usage
CMHeadphoneMotionManager.authorizationStatus()
//Initialize the manager and set the delegate
let motionManager = CMHeadphoneMotionManager()
motionManager.delegate = self
//Start tracking (simplified)
//Implement the device motion handler callback to receive motion data
if !motionManager.isDeviceMotionActive {
motionManager.startDeviceMotionUpdates(to
queue: OperationQueue,
withHandler
handler: @escaping CMHeadphoneMotionManager.DeviceMotionHandler)
}
//Don't forget to implement the delegates to cover connecting/disconnecting headphones
func headphoneMotionManagerDidConnect(_ manager: CMHeadphoneMotionManager) {}
func headphoneMotionManagerDidDisconnect(_ manager: CMHeadphoneMotionManager) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment