PyObjC BrowserEngineKit Bindings
PyObjC provides Python bindings for the BrowserEngineKit framework on macOS, allowing Python applications to embed web content rendering capabilities. It is part of the larger PyObjC project, which wraps many macOS frameworks. Version 12.1 is current, with releases typically aligning with macOS SDK updates and Python version support changes.
Warnings
- breaking Python 3.9 support was dropped in PyObjC 12.0. Python 3.8 support was dropped in PyObjC 11.0. Future versions will continue to drop support for older Python versions as they reach end-of-life.
- breaking PyObjC 11.1 introduced changes to how Objective-C Automatic Reference Counting (ARC) rules are modeled for 'init' family methods, aligning with `clang`'s documentation. This means `init` methods now correctly 'steal' a reference to `self` and return a new one, potentially affecting custom memory management or object lifecycle expectations.
- breaking PyObjC 10.3 changed the interaction between Python's `__init__` and `__new__` methods. While 10.3.1 partially reverted this for user-implemented `__new__` methods, code relying on `__new__` provided by PyObjC cannot use `__init__`.
- gotcha PyObjC does not support Python's experimental free-threading (PEP 703) in Python 3.13, despite binary wheels being available. Running PyObjC in a free-threaded Python environment may lead to instability.
- breaking `objc.classAddMethods` no longer supports passing arbitrary callable types (e.g., `functools.partial`) as method implementations. Only `MethodType` or `FunctionType` values are accepted.
- breaking Entire frameworks that were deprecated by Apple have been removed from PyObjC bindings. For example, `IMServicePlugIn` was removed in PyObjC 10.0.
Install
-
pip install pyobjc-framework-browserenginekit -
pip install pyobjc
Imports
- BEBrowserViewController
from BrowserEngineKit import BEBrowserViewController
Quickstart
import objc
from AppKit import NSApplication, NSWindow, NSMakeRect
from BrowserEngineKit import BEBrowserViewController
from PyObjCTools import AppHelper # Common for PyObjC GUI apps
class MyBrowserViewController(BEBrowserViewController):
# A minimal subclass just to demonstrate extending BEBrowserViewController
pass
class AppDelegate(objc.NSObject):
def applicationDidFinishLaunching_(self, notification):
self.mainWindow = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
NSMakeRect(100, 100, 800, 600),
4 | 8 | 1 | 2, # Titled, Closable, Miniaturizable, Resizable
2, # NSBackingStoreBuffered
False
)
self.mainWindow.setTitle_("PyObjC BrowserEngineKit Demo")
self.browserVC = MyBrowserViewController.alloc().init()
self.mainWindow.setContentView_(self.browserVC.view())
self.mainWindow.makeKeyAndOrderFront_(None)
def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
app.setDelegate_(delegate)
AppHelper.runEventLoop() # Starts the macOS event loop
if __name__ == "__main__":
main()