Skip to content

Instantly share code, notes, and snippets.

@aaronpearce
Created February 18, 2022 03:08
Show Gist options
  • Save aaronpearce/668e9ac36ab45e2ff841e75d0884e419 to your computer and use it in GitHub Desktop.
Save aaronpearce/668e9ac36ab45e2ff841e75d0884e419 to your computer and use it in GitHub Desktop.
Detect Apple Remote model
//
// AppleRemote.swift
// HomeCamTV2
//
// Created by Aaron Pearce on 18/02/22.
// Copyright © 2022 Pearce Media Limited. All rights reserved.
//
import GameController
class AppleRemote {
static let shared = AppleRemote()
var current: Model = .siriRemoteG2
enum Model {
case siriRemoteG1
case siriRemoteG2
case controlCenterRemote
enum BackButtonStyle: String {
case menu, back
}
var description: String {
switch self {
case .siriRemoteG1:
return "Siri Remote (1st Generation)"
case .siriRemoteG2:
return "Siri Remote (2nd Generation)"
case .controlCenterRemote:
return "Control Center Remote"
}
}
var backButtonStyle: BackButtonStyle {
switch self {
case .siriRemoteG1:
return .menu
case .siriRemoteG2, .controlCenterRemote:
return .back
}
}
}
init() {
registerForGameControllerNotifications()
if let controller = GCController.current {
updateCurrent(for: controller)
}
}
private func registerForGameControllerNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeCurrent(_:)), name: .GCControllerDidBecomeCurrent, object: nil)
}
@objc private func didBecomeCurrent(_ notification: Notification) {
guard let controller = notification.object as? GCController else {
return
}
updateCurrent(for: controller)
}
private func updateCurrent(for controller: GCController) {
guard controller.microGamepad != nil else {
return
}
if #available(tvOS 15.0, *) {
switch controller.productCategory {
case GCProductCategorySiriRemote1stGen:
self.current = .siriRemoteG1
case GCProductCategorySiriRemote2ndGen:
self.current = .siriRemoteG2
case GCProductCategoryControlCenterRemote:
self.current = .controlCenterRemote
default:
self.current = .siriRemoteG2
}
} else {
if controller.motion != nil {
self.current = .siriRemoteG1
} else {
self.current = .siriRemoteG2
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment