ios 如何从 API 检索 iPhone IDFA?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12944504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:48:59  来源:igfitidea点击:

How to retrieve iPhone IDFA from API?

iosiphonetvosidfa

提问by fvisticot

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

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

回答by Zachary Drake

First of all:

首先:

#import <AdSupport/ASIdentifierManager.h> 

If you would like to get it as an NSString, use:

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

[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]

So your code might look like this:

所以你的代码可能是这样的:

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

回答by RaffAl

You first have to check if user user has decided to opt out from ad tracking. Only if he allowed it you can use the IDFA.

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

You can check it by calling isAdvertisingTrackingEnabledmethod of ASIdentifierManager.

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

isAdvertisingTrackingEnabled

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

isAdvertisingTrackingEnabled

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

The following code snippet shows how to obtain a string value of IDFA.

以下代码片段显示了如何获取 的字符串值IDFA

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
}

回答by CodaFi

ASIdentifierManageris the official way to garner the Advertising Identification Number from a device running iOS 6+. You can use -[[ASIdentifierManager sharedManager] advertisingIdentifier];to get it.

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

回答by Amro Shafie

Get IDFA in Swift:

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

回答by Vlad

Beginning in iOS 10, when a user enables “Limit Ad Tracking,” the OS will send along the advertising identifier with a new value of “00000000-0000-0000-0000-000000000000.”

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

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

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

回答by Jeehut

Here's a commented helper class in Swiftthat will give you a nilobject for the identifier if the user has turned advertisement tracking off:

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

Just add it to your project(for example in a file named IDFA.swift) and link the AdSupport.frameworkin your target via the "Linked Frameworks and Libraries" section in the General settings tab.

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

Then you can use it like this:

然后你可以像这样使用它

if let identifier = IDFA.shared.identifier {
    // use the identifier
} else {
    // put any fallback logic in here
}

回答by Oscar

Swift 3 & 4

斯威夫特 3 & 4

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

回答by J.D. Wooder

A nicer approach to get the IDFA or nil if tracking is disabled via iOS Setting is using a (private) extension:

如果通过 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 ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
            return ASIdentifierManager.shared().advertisingIdentifier.uuidString
        }

        return nil 
    }        
}

回答by worthbak

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

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

import AdSupport

...

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

回答by black_pearl

Swift 5with encapsulation:

带封装的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
     }
}