GCControllerDidConnect notification not received in VisionOS 2.0

I am unable to get VisionOS 2.0 (simulator) to receive the GCControllerDidConnect notification and thus am unable to setup support for a gamepad. However, it works in VisionOS 1.2.

For VisionOS 2.0 I've tried adding:

  • .handlesGameControllerEvents(matching: .gamepad) attribute to the view
  • Supports Controller User Interaction to Info.plist
  • Supported game controller types -> Extended Gamepad to Info.plist

...but the notification still doesn't fire. It does when the code is run from VisionOS 1.2 simulator, both of which have the Send Game Controller To Device option enabled.

Here is the example code. It's based on the Xcode project template. The only files updated were ImmersiveView.swift and Info.plist, as detailed above:

import SwiftUI
import GameController
import RealityKit
import RealityKitContent

struct ImmersiveView: View {

	var body: some View {
		RealityView { content in
			// Add the initial RealityKit content
			if let immersiveContentEntity = try? await Entity(named: "Immersive", in: realityKitContentBundle) {
				content.add(immersiveContentEntity)
			}
			NotificationCenter.default.addObserver(
				forName: NSNotification.Name.GCControllerDidConnect,
				object: nil, queue: nil) { _ in
					print("Handling GCControllerDidConnect notification")
				}
			
		}
		.modify {
			if #available(visionOS 2.0, *) {
				$0.handlesGameControllerEvents(matching: .gamepad)
			} else {
				$0
			}
		}
	}
}

extension View {
	func modify<T: View>(@ViewBuilder _ modifier: (Self) -> T) -> some View {
		return modifier(self)
	}
}

This should be resolved in visionOS 2.2 beta 1.

Hello,

I've tested this and am still unable to get it to work, with the following focused test:

import GameController
import SwiftUI

struct ContentView: View {
	
	@State private var gamepadNotification: NSObjectProtocol? = nil
	@State private var keyboardNotification: NSObjectProtocol? = nil
	@FocusState var focused
	
    var body: some View {
        VStack {
            Text("Hello, world!")
        }
		.focused($focused, equals: true)
		.handlesGameControllerEvents(matching: .gamepad)
		.task {
			try? await Task.sleep(for: .seconds(1))
			focused = true
			print("Setting up notification for controller connections")
			gamepadNotification = NotificationCenter.default.addObserver(
				forName: NSNotification.Name.GCControllerDidConnect,
				object: nil, queue: nil) { note in
					print("Handling GCControllerDidConnect notification RV")
				}

			keyboardNotification = NotificationCenter.default.addObserver(
				forName: NSNotification.Name.GCKeyboardDidConnect,
				object: nil, queue: nil) { note in
					print("Handling GCKeyboardDidConnect notification RV")
				}
			
			GCController.startWirelessControllerDiscovery{}
		}
    }
}

I have set up the following in info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>UIApplicationSceneManifest</key>
	<dict>
		<key>UIApplicationPreferredDefaultSceneSessionRole</key>
		<string>UIWindowSceneSessionRoleApplication</string>
		<key>UIApplicationSupportsMultipleScenes</key>
		<true/>
		<key>UISceneConfigurations</key>
		<dict/>
	</dict>
	<key>GCSupportsControllerUserInteraction</key>
	<true/>
	<key>GCSupportedGameControllers</key>
	<array>
		<dict>
			<key>ProfileName</key>
			<string>MicroGamepad</string>
		</dict>
		<dict>
			<key>ProfileName</key>
			<string>ExtendedGamepad</string>
		</dict>
		<dict>
			<key>ProfileName</key>
			<string>DirectionalGamepad</string>
		</dict>
	</array>
</dict>
</plist>

On vision OS 2.1 I am seeing the following on the console:

Setting up notification for controller connections Handling GCKeyboardDidConnect notification RV

i.e. I can setup the keyboard connection handler, but not the gamepad connection handler

———————————————————————————————————————— On vision OS 2.2 I am seeing the following on the console:

Setting up notification for controller connections

i.e. now the keyboard connection notification is not working.

Even though I am not able to setup either a keyboard nor gamepad connection event handler I am however able to control the simulator with the controller. i.e. the controller is connected and can pan and move the simulator with the controllers joysticks.

————————————————————————————————————————

Here’s the version numbers of the dev tools:

Simulator: Version 16.0 (1038), SimulatorKit 942, CoreSimulator 993.7 XCode: Version 16.2 (16C5031c) visionOS on simulator: visionOS 2.2 (22N840)

GCControllerDidConnect notification not received in VisionOS 2.0
 
 
Q