{"id":6069,"library":"pyobjc-framework-authenticationservices","title":"PyObjC AuthenticationServices Framework","description":"PyObjC is a bridge between Python and Objective-C, enabling Python developers to write full-featured macOS Cocoa applications. The `pyobjc-framework-authenticationservices` package provides Python wrappers for the macOS `AuthenticationServices` framework. It is currently at version 12.1 and follows the release cadence of the broader PyObjC project, typically releasing new versions to align with macOS SDK updates and Python version support changes.","status":"active","version":"12.1","language":"en","source_language":"en","source_url":"https://github.com/ronaldoussoren/pyobjc","tags":["macOS","PyObjC","framework wrapper","Objective-C","AuthenticationServices","UI","Cocoa"],"install":[{"cmd":"pip install pyobjc-framework-authenticationservices","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"This package provides the core bridging functionality between Python and Objective-C and is a fundamental dependency for all PyObjC framework wrappers.","package":"pyobjc-core"}],"imports":[{"note":"The framework's symbols are directly exposed under the top-level `AuthenticationServices` module.","symbol":"AuthenticationServices","correct":"import AuthenticationServices"},{"symbol":"ASWebAuthenticationSession","correct":"from AuthenticationServices import ASWebAuthenticationSession"},{"note":"Core UI components for macOS applications, often needed for event loops or window management with UI-driven frameworks.","symbol":"NSApplication","correct":"from AppKit import NSApplication"}],"quickstart":{"code":"import objc\nfrom AppKit import NSApplication, NSApp, NSWindow, NSApplicationActivationPolicyRegular\nfrom Foundation import NSObject, NSURL, NSError\nfrom AuthenticationServices import (\n    ASWebAuthenticationSession,\n    ASWebAuthenticationSessionDelegate,\n    ASWebAuthenticationPresentationContextProviding,\n)\nimport os\n\n# Define a delegate for ASWebAuthenticationSession and its presentation context\nclass WebAuthSessionDelegate(\n    NSObject,\n    ASWebAuthenticationSessionDelegate,\n    ASWebAuthenticationPresentationContextProviding,\n):\n    def init(self):\n        self = objc.super(WebAuthSessionDelegate, self).init()\n        return self\n\n    # Required ASWebAuthenticationSessionDelegate methods\n    def authenticationSession_didCompleteWithCallbackURL_(self, session, callbackURL):\n        print(f\"Authentication session completed with URL: {callbackURL.absoluteString()}\")\n        NSApp.terminate_(None)\n\n    def authenticationSession_didFailWithError_(self, session, error):\n        print(f\"Authentication session failed with error: {error.localizedDescription()}\")\n        NSApp.terminate_(None)\n\n    def authenticationSession_didCancel_(self, session):\n        print(\"Authentication session cancelled.\")\n        NSApp.terminate_(None)\n\n    # Required ASWebAuthenticationPresentationContextProviding method\n    def presentationAnchorForWebAuthenticationSession_(self, session):\n        # In a real GUI app, this would return the main window of your application.\n        # For a minimal example, we'll try to get the key window or create a dummy one.\n        # Note: The application needs to be activated for a window to be meaningfully presented.\n        if NSApp.keyWindow():\n            return NSApp.keyWindow()\n        else:\n            # Fallback for headless environments or early stages; may not always work perfectly\n            # Create a minimal, temporary window if no key window is available.\n            # This might not be suitable for all presentation contexts.\n            window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(((0,0),(1,1)), 0, 0, False)\n            window.makeKeyAndOrderFront_(None)\n            return window\n\ndef main():\n    # Establish a shared NSApplication instance\n    if not NSApp:\n        _ = NSApplication.sharedApplication()\n    NSApp.setActivationPolicy_(NSApplicationActivationPolicyRegular)\n\n    delegate_instance = WebAuthSessionDelegate.alloc().init()\n\n    # Dummy URLs for demonstration purposes\n    auth_url = NSURL.URLWithString_(\"https://www.example.com/auth\")\n    callback_scheme = \"myapp\" # e.g., 'myapp://callback'\n\n    # Create the authentication session\n    session = ASWebAuthenticationSession.alloc().initWithURL_callbackURLScheme_completionHandler_(\n        auth_url,\n        callback_scheme,\n        None # We use the delegate methods for completion/failure\n    )\n\n    # Set the delegate for both completion and presentation context\n    session.setDelegate_(delegate_instance)\n    session.setPresentationContextProvider_(delegate_instance)\n\n    # Start the session\n    print(\"Starting ASWebAuthenticationSession...\")\n    if not session.start():\n        print(\"Failed to start ASWebAuthenticationSession.\")\n        NSApp.terminate_(None)\n\n    # Run the application event loop until terminated by the delegate\n    NSApp.run()\n\nif __name__ == \"__main__\":\n    main()","lang":"python","description":"This quickstart demonstrates how to initiate a web authentication session using `ASWebAuthenticationSession` from the `AuthenticationServices` framework. It sets up a minimal `NSApplication` and defines a delegate to handle the authentication session's completion, failure, or cancellation, which are crucial for interactive macOS frameworks. The `presentationAnchorForWebAuthenticationSession_` method provides the necessary display context for the authentication UI."},"warnings":[{"fix":"Upgrade your Python environment to 3.10 or a newer compatible version.","message":"PyObjC 12.0 dropped support for Python 3.9. Users must upgrade to Python 3.10 or later.","severity":"breaking","affected_versions":">=12.0"},{"fix":"Upgrade your Python environment to 3.9 or a newer compatible version.","message":"PyObjC 11.0 dropped support for Python 3.8. Users must upgrade to Python 3.9 or later (though 3.10+ is recommended for 12.x).","severity":"breaking","affected_versions":">=11.0"},{"fix":"Review code that interacts with Objective-C `init` methods and ensure correct reference counting, especially if manual memory management patterns were used in Python.","message":"PyObjC 11.1 introduced alignment with clang's Automatic Reference Counting (ARC) documentation for `init` methods. Specifically, methods in the 'init' family now correctly model that they steal a reference to `self` and return a new reference. Code relying on previous, potentially incorrect, reference counting behavior for `init` methods might need adjustment.","severity":"gotcha","affected_versions":">=11.1"},{"fix":"If experiencing issues with `__init__` in conjunction with `__new__` on PyObjC 10.3, upgrade to 10.3.1 or later. Be mindful of the `__new__` implementation source.","message":"In PyObjC 10.3, a change briefly broke the ability to use `__init__` in Python subclasses when `__new__` was also custom-implemented (either in the class or a superclass). This was re-enabled in 10.3.1. However, if relying on the `__new__` provided by PyObjC, `__init__` cannot be used.","severity":"gotcha","affected_versions":"10.3"},{"fix":"Upgrade your Python environment to 3.8 or a newer compatible version.","message":"PyObjC 10.0 dropped support for Python 3.7. Users must upgrade to Python 3.8 or later (though newer versions are recommended for current PyObjC releases).","severity":"breaking","affected_versions":">=10.0"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}