PyObjC DeviceCheck Framework

12.1 · active · verified Tue Apr 14

PyObjC-framework-DeviceCheck provides Python wrappers for Apple's DeviceCheck framework on macOS, allowing Python applications to interact with DeviceCheck APIs to manage device state and assert app integrity. It is part of the broader PyObjC project, a bridge that enables Python scripts to fully utilize Objective-C class libraries like Cocoa. The library is actively maintained, with version 12.1 being the current release, and follows a frequent release cadence tied to macOS SDK and Python version updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to access the current DCDevice, check if DeviceCheck is supported, and then generate a device token using an asynchronous Objective-C method. A Python function is used as the completion handler to process the token or any errors, and the PyObjCTools.AppHelper is used to manage the event loop necessary for asynchronous Objective-C calls in a script.

import objc
from DeviceCheck import DCDevice
from PyObjCTools import AppHelper
import Foundation # For NSData, NSError types

def device_check_completion_handler(token_data, error):
    """Objective-C completion handler for generateTokenWithCompletionHandler_."""
    if error:
        print(f"Error generating token: {error.localizedDescription()}")
    elif token_data:
        # NSData object from Objective-C can be converted to Python bytes
        python_token = token_data.bytes().tobytes()
        print(f"Successfully generated DeviceCheck token (first 10 bytes): {python_token[:10]}...")
    else:
        print("No token data and no error received.")
    AppHelper.stopEventLoop()

def main():
    device = DCDevice.currentDevice()

    if not device.isSupported():
        print("DeviceCheck is not supported on this device (e.g., simulator).")
        return

    print("DeviceCheck is supported. Generating token...")
    # Call the asynchronous Objective-C method with a Python callable as handler
    device.generateTokenWithCompletionHandler_(device_check_completion_handler)
    
    # Start the PyObjC event loop to process the asynchronous call
    print("Starting event loop (waiting for token or error)...")
    AppHelper.runEventLoop()
    print("Event loop stopped.")

if __name__ == "__main__":
    main()

view raw JSON →