PyObjC AdSupport Framework
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
- breaking PyObjC has consistently dropped support for older Python versions with major releases. PyObjC 12.0 dropped Python 3.9, and PyObjC 11.0 dropped Python 3.8. Users should verify their Python version compatibility when upgrading PyObjC.
- breaking Version 11.1 aligned PyObjC's behavior with `clang`'s Automatic Reference Counting (ARC) documentation for initializer methods. This change can affect how references are managed, potentially leading to crashes if code relies on previous reference counting behavior, especially around `alloc()` and `init` methods. The `isAlloc` attribute of `objc.selector` is deprecated and will be removed in PyObjC 12.
- gotcha Merely referencing the `ASIdentifierManager` class (e.g., to check if the framework is linked) can cause the AdSupport framework to be linked by your application. This can lead to app rejections from Apple if your app doesn't actually use IDFA for advertising AND doesn't implement App Tracking Transparency.
- gotcha Initial PyObjC 10.3 versions broke `__init__` usage when combined with PyObjC-provided `__new__` for Objective-C subclasses. While 10.3.1 partially reverted this for user-implemented `__new__`, directly calling `__init__` on `alloc()`'d objects without a user-defined `__new__` might still be problematic.
- gotcha Installation of PyObjC, including framework wrappers, often relies on Xcode Command Line Tools being installed on macOS. Missing tools can lead to compilation errors during `pip install`.
- breaking Frameworks and specific symbols can be deprecated or entirely removed as Apple updates macOS SDKs. For example, the `IMServicePlugIn` framework bindings were removed in PyObjC 10.0 because the underlying framework was removed in macOS 14.
Install
-
pip install pyobjc-framework-adsupport
Imports
- AdSupport
import AdSupport
Quickstart
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}")