PyObjC AdServices Framework

12.1 · active · verified Tue Apr 14

PyObjC provides Python wrappers for the "AdServices" framework on macOS, allowing Python scripts to interact with Apple's Objective-C APIs for ad attribution. This specific package, `pyobjc-framework-adservices`, enables access to the AdServices framework. PyObjC is actively maintained with frequent releases, often synchronized with macOS SDK updates, ensuring compatibility with the latest Apple operating systems and developer tools. The current version is 12.1.

Warnings

Install

Imports

Quickstart

This example demonstrates how to request an attribution token using Apple's AdServices framework via PyObjC. It defines a completion handler to process the token or any errors, and uses `PyObjCTools.AppHelper.runEventLoop()` to manage the asynchronous callback.

import AdServices
import objc
from Foundation import NSLog
from PyObjCTools import AppHelper

def attribution_completion_handler(token, error):
    """Callback function for the attribution token request."""
    if error:
        NSLog("Error requesting AdServices attribution token: %@", error)
    else:
        NSLog("AdServices Attribution Token: %@", token)
    AppHelper.stopEventLoop() # Stop the event loop after receiving the token

def request_token():
    """Initiates the request for an AdServices attribution token."""
    NSLog("Requesting AdServices attribution token...")
    # The Objective-C method 'requestAttributionTokenWithCompletionHandler:'
    # translates to 'requestAttributionTokenWithCompletionHandler_' in PyObjC
    # (note the trailing underscore for methods that take blocks/callbacks).
    AdServices.AAAttribution.requestAttributionTokenWithCompletionHandler_(attribution_completion_handler)

if __name__ == "__main__":
    # Call the request function directly
    request_token()
    # For asynchronous operations (like network requests with callbacks),
    # a run loop is often needed to process the callback.
    # AppHelper.runEventLoop() keeps Python running until stopEventLoop() is called.
    AppHelper.runEventLoop()

view raw JSON →