How to get iPhone IDFA from API?

I would like to receive an IDFA device. How to get this information from the official iOS API?

+49
ios iphone tvos idfa
Oct 17 '12 at 10:40
source share
8 answers

Primarily:

 #import <AdSupport/ASIdentifierManager.h> 

If you want to get it as an NSString, use:

 [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString] 

So your code might look like this:

 NSString *idfaString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; 
+67
Oct 03 '13 at 19:50
source share

First you need to check if the user decided to opt out of tracking ads . Only if he allowed this can you use the IDFA .

You can verify this by calling the isAdvertisingTrackingEnabled ASIdentifierManager method.

isAdvertisingTrackingEnabled

Check the value of this property before performing any ad tracking. If the value is NO , use the advertising identifier only for the following purposes: frequency limitation, conversion events, evaluating the number of unique users, security and fraud detection, and debugging.

The following code snippet shows how to get the IDFA string value.

Objc

 @import AdSupport; - (NSString *)identifierForAdvertising { // Check whether advertising tracking is enabled if([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) { NSUUID *identifier = [[ASIdentifierManager sharedManager] advertisingIdentifier]; return [identifier UUIDString]; } // Get and return IDFA return nil; } 

Swift

 import AdSupport func identifierForAdvertising() -> String? { // Check whether advertising tracking is enabled guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else { return nil } // Get and return IDFA return ASIdentifierManager.shared().advertisingIdentifier.uuidString } 
+39
Apr 22 '14 at 10:18
source share

ASIdentifierManager is the official way to get the advertising ID from an iOS 6+ device. You can use -[[ASIdentifierManager sharedManager] advertisingIdentifier]; , to get it.

+13
Oct 17
source share

Get IDFA in Swift:

  import AdSupport ... let myIDFA: String? // Check if Advertising Tracking is Enabled if ASIdentifierManager.sharedManager().advertisingTrackingEnabled { // Set the IDFA myIDFA = ASIdentifierManager.sharedManager().advertisingIdentifier.UUIDString } else { myIDFA = nil } 
+10
Apr 23 '15 at 19:39
source share

Starting with iOS 10, when the user allows "tracking of final ads", the OS sends an advertising identifier with a new value of "00000000-0000-0000-0000000000000000".

According to this article: https://fpf.org/2016/08/02/ios-10-feature-stronger-limit-ad-tracking/

+3
Sep 09 '16 at 23:27
source share

Swift 3 and 4

 var IDFA = String() if ASIdentifierManager.shared().isAdvertisingTrackingEnabled { IDFA = ASIdentifierManager.shared().advertisingIdentifier } 
+3
Jul 31 '17 at 13:26
source share

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 } 
+2
Feb 06 '17 at 10:27
source share

Just to extend Amro Swift's answer, here similar code is wrapped in a method:

 import AdSupport ... func provideIdentifierForAdvertisingIfAvailable() -> String? { if ASIdentifierManager.sharedManager().advertisingTrackingEnabled { return ASIdentifierManager.sharedManager().advertisingIdentifier?.UUIDString ?? nil } else { return nil } } 
+1
Sep 24 '15 at 14:26
source share



All Articles