PyObjC LocalAuthenticationEmbeddedUI Framework
This library provides Python wrappers for Apple's LocalAuthenticationEmbeddedUI framework on macOS. It's part of the larger PyObjC project, which serves as a bridge between Python and Objective-C, enabling Python scripts to interact with and extend macOS Cocoa libraries. The project is actively maintained with frequent releases.
Warnings
- breaking PyObjC v12.0 dropped support for Python 3.9. Users on Python 3.9 or earlier should use PyObjC v11.x or upgrade their Python version.
- breaking PyObjC v11.0 dropped support for Python 3.8. Users on Python 3.8 or earlier should use PyObjC v10.x or upgrade their Python version.
- breaking PyObjC v11.1 changed the reference counting behavior for initializer methods to align with `clang`'s Automatic Reference Counting (ARC) documentation. This might alter how references to `self` are handled during object initialization.
- gotcha PyObjC framework wrappers, including LocalAuthenticationEmbeddedUI, generally do not include their own documentation. Developers must refer to Apple's official documentation for the `LocalAuthenticationEmbeddedUI` framework to understand API usage, class methods, and protocols.
- gotcha As of PyObjC v10.3, the library does not support the experimental free-threading feature introduced in Python 3.13. Running PyObjC with free-threading enabled may lead to undefined behavior or crashes.
- gotcha In v10.1, the behavior of `os.fspath()` for Cocoa URLs (NSURL, CFURLRef) referring to local filesystem paths was changed. It now works correctly, while `TypeError` is raised for other URLs.
Install
-
pip install pyobjc-framework-localauthenticationembeddedui
Imports
- LAAuthenticationView
from LocalAuthenticationEmbeddedUI import LAAuthenticationView
- LAAuthenticationViewDelegate
from LocalAuthenticationEmbeddedUI import LAAuthenticationViewDelegate
- LAContext
from LocalAuthentication import LAContext
Quickstart
import objc
from AppKit import NSApplication, NSWindow, NSView, NSRect, NSButton, NSAppDelegate, NSApp
from Foundation import NSObject, NSSelectorFromString, NSLog
from LocalAuthenticationEmbeddedUI import LAAuthenticationView, LAAuthenticationViewDelegate
from LocalAuthentication import LAContext, LAPolicy, LAError
class AuthenticationDelegate(NSObject, LAAuthenticationViewDelegate):
def authenticationView_didCompleteWithResult_error_(self, view, result, error):
if error:
NSLog(f"Authentication failed with error: {error.localizedDescription()}")
if error.code() == LAError.UserCancel: # LAError.UserCancel is part of LAError, not LAError.Code.UserCancel
NSLog("User cancelled authentication.")
elif result:
if result == 1: # Assuming LAAuthenticationResult.Success is 1. Check Apple docs for exact enum values.
NSLog("Authentication successful!")
else:
NSLog("Authentication completed with unknown result.")
else:
NSLog("Authentication completed, but no result or error.")
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
# Create a window
self.window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
NSRect((200, 300), (400, 200)),
(1 << 0 | 1 << 1 | 1 << 3 | 1 << 8), # Titled, Closable, Miniaturizable, Resizable
2, # NSBackingStoreBuffered
False
)
self.window.setTitle_("PyObjC LAEmbeddedUI Example")
self.window.makeKeyAndOrderFront_(None)
# Create a content view
content_view = NSView.alloc().init().autorelease()
self.window.setContentView_(content_view)
# Create LAAuthenticationView
self.auth_view = LAAuthenticationView.alloc().initWithFrame_(NSRect((50, 50), (300, 100)))
content_view.addSubview_(self.auth_view)
# Set delegate and context
self.delegate = AuthenticationDelegate.alloc().init()
self.auth_view.setDelegate_(self.delegate)
# Create LAContext (from LocalAuthentication framework)
self.la_context = LAContext.alloc().init()
self.auth_view.setContext_(self.la_context)
# Start authentication (LAAuthenticationView will start when added to view hierarchy with context)
# It might also need an explicit call like evaluatePolicy_localizedReason_reply_ from LAContext
# but for embedded UI, setting the context and delegate is often enough to trigger its display.
NSLog("LAAuthenticationView added. Awaiting user interaction.")
if __name__ == '__main__':
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
app.run()