PyObjC GameKit Framework
PyObjC is a bridge between Python and Objective-C, allowing Python scripts to use and extend existing Objective-C class libraries, including macOS frameworks. The `pyobjc-framework-gamekit` package provides Python wrappers for the GameKit framework on macOS, enabling Python applications to leverage GameKit functionalities. It is currently at version 12.1 and is actively maintained with releases often tied to macOS SDK updates.
Warnings
- breaking PyObjC 12.0 dropped support for Python 3.9, and PyObjC 11.0 dropped support for Python 3.8. Ensure your Python version is 3.10 or higher for PyObjC 12.x.
- breaking In PyObjC 10.3, the behavior around `__init__` and `__new__` for Python subclasses of Objective-C classes changed significantly, potentially breaking existing code. While 10.3.1 partially reintroduced `__init__` support when `__new__` is implemented by the user, code relying on PyObjC's `__new__` cannot use `__init__`. PyObjC 10.4 introduced a more Pythonic instantiation interface, making `SomeClass(...)` equivalent to `SomeClass.alloc().init...()`.
- breaking PyObjC 11.1 aligned its behavior for initializer methods (those in the 'init' family) with `clang`'s Automatic Reference Counting (ARC) documentation. This means `init` methods now correctly steal a reference to `self` and return a new reference, changing previous `[NSObject alloc]` proxy behavior.
- gotcha While PyObjC 10.3 and later may have binary wheels for Python 3.13, PyObjC does *not* currently support the experimental free-threading feature (PEP 703) in Python 3.13.
- gotcha Prior to PyObjC 10.1, `os.fspath(someURL)` would not correctly handle Cocoa URLs (`NSURL`, `CFURLRef`) referring to local filesystem paths, raising `TypeError` for other URLs. This was fixed in 10.1.
- gotcha Before PyObjC 12.1, there was an issue where Key-Value Observing (KVO) usage for subclasses of `NSProxy` defined in Python was not automatically disabled, which has since been fixed.
Install
-
pip install pyobjc-framework-gamekit
Imports
- GKMatch
from GameKit import GKMatch
- NSString
from Foundation import NSString
Quickstart
import Foundation
# Basic Objective-C object creation using the PyObjC bridge
my_string = Foundation.NSString.stringWith_("Hello from PyObjC!")
print(f"Created NSString: {my_string}")
print(f"Type of my_string: {type(my_string)}")
# To interact with the GameKit framework, you would import it similarly:
try:
import GameKit
print("GameKit imported successfully.")
# Example: Accessing a GameKit class (this shows access, but doesn't 'do' anything game-related)
_ = GameKit.GKMatch
print(f"Accessed GameKit.GKMatch class.")
except ImportError:
print("GameKit framework not available or could not be imported.")
except Exception as e:
print(f"Error accessing GameKit: {e}")
# For applications needing an event loop (e.g., actual GUI or complex GameKit interactions),
# PyObjCTools.AppHelper.runEventLoop() is typically used, but requires a full application context.
# This example just demonstrates basic object interaction and framework import.