Here's a commented helper class in Swift that will give you a nil object for an id if the user has disabled ad tracking:
import AdSupport class IDFA { // MARK: - Stored Type Properties static let shared = IDFA() // MARK: - Computed Instance Properties /// Returns `true` if the user has turned off advertisement tracking, else `false`. var limited: Bool { return !ASIdentifierManager.shared().isAdvertisingTrackingEnabled } /// Returns the identifier if the user has turned advertisement tracking on, else `nil`. var identifier: String? { guard !limited else { return nil } return ASIdentifierManager.shared().advertisingIdentifier.uuidString } }
Just add it to your project (for example, in a file called IDFA.swift ) and place AdSupport.framework in your target through the "Linked Frames and Libraries" section of the "General Settings" tab.
Then you can use it like this ::
if let identifier = IDFA.shared.identifier { // use the identifier } else { // put any fallback logic in here }
Dschee Feb 06 '17 at 10:27 2017-02-06 10:27
source share