如何从API检索iPhone IDFA?


Answers:


90

首先:

#import <AdSupport/ASIdentifierManager.h> 

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

[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]

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

NSString *idfaString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

13
切记:#import <AdSupport / ASIdentifierManager.h>
parsley72

@ parsley72,您可以使用“ @import AdSupport;” 相反,因此您没有添加框架的额外步骤。编辑了答案以包括此内容。
vinnybad

3
使用Swift :,import AdSupport然后ASIdentifierManager.sharedManager().advertisingIdentifier获取IDFA!
nodebase 2016年

1
请注意,在iOS 10上,如果用户启用了限制广告跟踪,则该IDFA在物理设备上将为全零。(在Xcode 8 GM种子的模拟器中,它似乎仍然返回任意IDFA。)
Zachary Drake

请注意,很快(从2020年9月开始),您将必须征得用户许可才能使用IDFA。您必须使用App Tracking Transparency来做到这一点:developer.apple.com/documentation/apptrackingtransparency
Zachary Drake

75

首先,您必须检查用户用户是否已决定退出广告跟踪。只有他允许,您才能使用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
}

1
我必须为AdMob使用IDFA吗?
Roger CS Wernersson 2015年

14

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


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
    }

7

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

很酷,但是您不需要将IDFA作为单例使用,ASIdentifierManager已经是一个单例,因此,只需将var设为var类即可直接使用它们
busta117


6

斯威夫特3&4

var IDFA = String()
if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
            IDFA = ASIdentifierManager.shared().advertisingIdentifier
}

3

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
}

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

1

只是为了扩展Amro的Swift答案,这是一个包装在方法中的类似代码:

import AdSupport

...

func provideIdentifierForAdvertisingIfAvailable() -> String? {
    if ASIdentifierManager.sharedManager().advertisingTrackingEnabled {
      return ASIdentifierManager.sharedManager().advertisingIdentifier?.UUIDString ?? nil
    } else {
      return nil
    }
  }

0

Swift 5带封装:

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
     }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.