PyObjC AdSupport Framework

12.1 · active · verified Tue Apr 14

PyObjC-framework-adsupport provides Python bindings for Apple's AdSupport framework on macOS. It allows Python applications to interact with native macOS APIs to obtain the advertising identifier (IDFA), which is unique to each device and primarily used for advertising purposes. The library is part of the larger PyObjC project, which regularly releases updates, with major versions typically aligning with macOS SDK changes and Python version support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to access the shared instance of `ASIdentifierManager` and retrieve the `advertisingIdentifier`. It also includes a note about `App Tracking Transparency` requirements for obtaining a meaningful IDFA on modern iOS/iPadOS versions.

import AdSupport
from Foundation import NSUUID

# Note: On iOS 14.5+ and iPadOS 14.5+, your app must request
# tracking authorization using the AppTrackingTransparency framework
# (e.g., AppTrackingTransparency.ATTrackingManager.requestTrackingAuthorization_)
# before a non-zero advertisingIdentifier can be obtained.
# For macOS, AdSupport is available from 10.14+.

manager = AdSupport.ASIdentifierManager.sharedManager()

if manager.isAdvertisingTrackingEnabled():
    advertising_id_nsuuid = manager.advertisingIdentifier()
    # Convert NSUUID to a Python string
    advertising_id_str = str(advertising_id_nsuuid.UUIDString())
    print(f"Advertising ID (IDFA): {advertising_id_str}")
else:
    print("Advertising tracking is disabled. Advertising ID will be all zeros.")
    # The ID will be '00000000-0000-0000-0000-000000000000'
    advertising_id_nsuuid = manager.advertisingIdentifier()
    advertising_id_str = str(advertising_id_nsuuid.UUIDString())
    print(f"Advertising ID (IDFA): {advertising_id_str}")

view raw JSON →