83

我想得到设备IDFA。如何从 iOS 官方 API 获取此信息?

4

12 回答 12

97

首先,您必须征得用户的许可才能使用他们的 IDFA:

#import <AppTrackingTransparency/AppTrackingTransparency.h> 

[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
 // Tracking authorization completed. Start loading ads here. 
 }];

此权限流只会在第一次调用时运行一次,即使您重新启动应用程序和/或再次调用它也是如此。如果您想以不同的方式回答,则必须从设备或模拟器中完全删除该应用程序并重新安装。请注意,无论权限流的答案是什么,iOS 模拟器都会返回空白 IDFA(全为零)。有关详细信息,请参阅https://developer.apple.com/documentation/apptrackingtransparency,包括如何自定义在要求跟踪用户时向用户显示的消息。请注意,许多广告 SDK 都有自己可以使用的同意流调用。

获得许可后,要真正获得 IDFA:

#import <AdSupport/ASIdentifierManager.h> 

如果您想将其作为 NSString 获取,请使用:

[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]

因此,您的代码可能如下所示:

NSString *idfaString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
NSLog (@"IDFA: %@", idfaString);
于 2013-10-03T19:50:30.893 回答
84

您首先必须检查用户用户是否决定退出广告跟踪。只有在他允许的情况下,您才能使用IDFA.

您可以通过调用isAdvertisingTrackingEnabled的方法来检查它ASIdentifierManager

isAdvertisingTrackingEnabled

在执行任何广告跟踪之前检查此属性的值。如果值为NO,则仅将广告标识符用于以下目的:频次上限、转化事件、估计唯一用户数、安全和欺诈检测以及调试。

以下代码片段显示了如何获取IDFA.

对象

@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;
}

迅速

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
}
于 2014-04-22T10:18:03.407 回答
17

IDFA- 广告标识符

isAdvertisingTrackingEnabled -> trackingAuthorizationStatus

iOS v14 Apple 弃用isAdvertisingTrackingEnabled并将逻辑移至AppTrackingTransparencyFramework。现在用户必须获得阅读权限idfa(与位置权限相同)

用户可以通过以下方式控制它:

#iOS 13
#AdSupport
#ASIdentifierManager.shared().isAdvertisingTrackingEnabled
Settings -> Privacy -> Advertising -> Limit Ad Tracking
#iOS 14
#AppTrackingTransparency
#ATTrackingManager.trackingAuthorizationStatus

#a global flag
Settings -> Privacy -> Tracking -> `Allow Apps to Request to Track` 
#or select an app from list to control every app separately 

#a local flag
Settings -> <app_name> -> Allow Tracking 

执行

import AppTrackingTransparency
import AdSupport
func getIDFA() -> String? {
    // Check whether advertising tracking is enabled
    if #available(iOS 14, *) {
        if ATTrackingManager.trackingAuthorizationStatus != ATTrackingManager.AuthorizationStatus.authorized  {
            return nil
        }
    } else {
        if ASIdentifierManager.shared().isAdvertisingTrackingEnabled == false {
            return nil
        }
    }

    return ASIdentifierManager.shared().advertisingIdentifier.uuidString
}
于 2020-09-21T14:57:33.597 回答
14

ASIdentifierManager是从运行 iOS 6+ 的设备获取广告标识号的官方方法。您可以使用-[[ASIdentifierManager sharedManager] advertisingIdentifier];它来获取它。

于 2012-10-17T22:53:27.407 回答
12

在 Swift 中获取 IDFA:

    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
    }
于 2015-04-23T19:39:13.857 回答
8

从 iOS 10 开始,当用户启用“限制广告跟踪”时,操作系统将发送带有新值“00000000-0000-0000-0000-000000000000”的广告标识符。</p>

根据这篇文章:https ://fpf.org/2016/08/02/ios-10-feature-stronger-limit-ad-tracking/

于 2016-09-09T23:27:10.437 回答
6

这是 Swift 中的一个带注释的帮助器类,nil如果用户关闭了广告跟踪,它将为您提供一个标识符对象:

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
    }
}

只需将其添加到您的项目中(例如在名为 的文件中IDFA.swift)并AdSupport.framework通过“常规设置”选项卡中的“链接的框架和库”部分将其链接到您的目标中。

然后你可以像这样使用它

if let identifier = IDFA.shared.identifier {
    // use the identifier
} else {
    // put any fallback logic in here
}
于 2017-02-06T10:27:45.363 回答
6

斯威夫特 3 & 4

var IDFA = String()
if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
            IDFA = ASIdentifierManager.shared().advertisingIdentifier
}
于 2017-07-31T13:26:20.487 回答
2

如果通过 iOS 设置禁用跟踪,则获取 IDFA 或 nil 的更好方法是使用(私有)扩展:

import AdSupport

class YourClass {

    func printIDFA() {
        print(ASIdentifierManager.shared().advertisingIdentifierIfPresent)
    }
}

private extension ASIdentifierManager {

    /// IDFA or nil if ad tracking is disabled via iOS system settings
    var advertisingIdentifierIfPresent: String? {
        if isAdvertisingTrackingEnabled {
            return advertisingIdentifier.uuidString
        }

        return nil 
    }        
}
于 2018-11-16T09:20:56.033 回答
2

请注意,在 iOS 14 中,ASIdentifierManager.shared().isAdvertisingTrackingEnabled已弃用。请ATTrackingManager.trackingAuthorizationStatus == .authorized改用。

import AdSupport
import AppTrackingTransparency

extension ASIdentifierManager {
    //NOTE: if the user has enabled Limit Ad Tracking, this IDFA will be all zeros on a physical device
    static var identifierForAdvertising: String {

        // Check whether advertising tracking is enabled
        if #available(iOS 14, *) {
            guard ATTrackingManager.trackingAuthorizationStatus == .authorized else {
                return ""
            }
        } else {
            guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else {
                return ""
            }
        }
            
        // Get and return IDFA
        return ASIdentifierManager.shared().advertisingIdentifier.uuidString
    }
}
于 2021-07-26T09:06:43.103 回答
1

只是为了扩展 Amro 的 Swift 答案,下面是包含在方法中的类似代码:

import AdSupport

...

func provideIdentifierForAdvertisingIfAvailable() -> String? {
    if ASIdentifierManager.sharedManager().advertisingTrackingEnabled {
      return ASIdentifierManager.sharedManager().advertisingIdentifier?.UUIDString ?? nil
    } else {
      return nil
    }
  }
于 2015-09-24T14:26:46.280 回答
0

Swift 5带封装:</p>

import AdSupport

struct ID{
    static var advertising: String? {
         // Firstly, Check whether advertising tracking is enabled
         guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else {
              return nil
         }
         // Then, Get and return IDFA
         return ASIdentifierManager.shared().advertisingIdentifier.uuidString
     }
}
于 2020-04-30T07:51:35.377 回答