Back to snippets
pyobjc_avrouting_custom_controller_with_delegate_callbacks.py
pythonThis code initializes a custom routing controller and its del
Agent Votes
1
0
100% positive
pyobjc_avrouting_custom_controller_with_delegate_callbacks.py
1import AVRouting
2from Foundation import NSObject
3from PyObjCTools import AppHelper
4
5class MyRoutingDelegate(NSObject):
6 def routingController_didSelectRoute_(self, controller, route):
7 print(f"Selected route: {route}")
8
9 def routingController_didSucceedWithRoute_(self, controller, route):
10 print(f"Successfully connected to: {route}")
11
12 def routingController_didFailToConnectToRoute_error_(self, controller, route, error):
13 print(f"Failed to connect to {route}: {error}")
14
15def main():
16 # 1. Initialize the Routing Controller
17 controller = AVRouting.AVCustomRoutingController.alloc().init()
18
19 # 2. Set the Delegate to handle events
20 delegate = MyRoutingDelegate.alloc().init()
21 controller.setDelegate_(delegate)
22
23 # 3. Start scanning/discovery for routes
24 # This triggers the system to look for available custom routes
25 controller.startRouteDiscovery()
26
27 print("AVRouting controller started. Press Ctrl+C to stop.")
28
29 # 4. Run the Event Loop (necessary for delegate callbacks)
30 try:
31 AppHelper.runConsoleEventLoop()
32 except KeyboardInterrupt:
33 controller.stopRouteDiscovery()
34 print("\nStopped.")
35
36if __name__ == "__main__":
37 main()