PyObjC LocalAuthenticationEmbeddedUI Framework

12.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate and use `LAAuthenticationView` for embedded biometric authentication within a basic macOS Cocoa application. It sets up an `NSApplication`, a window, and integrates the `LAAuthenticationView` with a custom delegate to handle authentication results. Note that `LAAuthenticationView` requires a running macOS application context to be visible and functional. The `LAContext` object is imported from the `LocalAuthentication` framework, which works in conjunction with the UI component. The exact enum values for `LAAuthenticationResult` might need to be verified against Apple's latest documentation.

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()

view raw JSON →