I would like to get the device IDFA
. How to get this info from iOS official API ?
我想买这个设备IDFA。如何从iOS官方API获取这个信息?
8 个解决方案
#1
73
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];
#2
47
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
.
您首先要检查用户是否决定退出ad跟踪。只要他允许,你就可以使用IDFA。
You can check it by calling isAdvertisingTrackingEnabled
method of ASIdentifierManager
.
您可以通过调用ASIdentifierManager的isAdvertisingTrackingEnabled方法来查看它。
isAdvertisingTrackingEnabled
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.在执行任何广告跟踪之前,检查该属性的价值。如果值为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
}
#3
13
ASIdentifierManager is 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]广告标识符];去得到它。
#4
10
Get IDFA in 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
}
#5
5
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- 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特性强-限制-广告跟踪/
#6
4
Here's a commented helper class in Swift that will give you a nil
object 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.framework
in your target via the "Linked Frameworks and Libraries" section in the General settings tab.
只需将其添加到您的项目中(例如,在名为IDFA.swift的文件中),并通过General settings选项卡中的“链接框架和库”部分链接到目标中的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
}
#7
3
Swift 3 & 4
斯威夫特3 & 4
var IDFA = String()
if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
IDFA = ASIdentifierManager.shared().advertisingIdentifier
}
#8
1
Just to extend Amro's Swift answer, here's similar code wrapped in a method:
为了扩展Amro银行的快速回答,这里有一种方法中包含的类似代码:
import AdSupport
...
func provideIdentifierForAdvertisingIfAvailable() -> String? {
if ASIdentifierManager.sharedManager().advertisingTrackingEnabled {
return ASIdentifierManager.sharedManager().advertisingIdentifier?.UUIDString ?? nil
} else {
return nil
}
}
#1
73
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];
#2
47
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
.
您首先要检查用户是否决定退出ad跟踪。只要他允许,你就可以使用IDFA。
You can check it by calling isAdvertisingTrackingEnabled
method of ASIdentifierManager
.
您可以通过调用ASIdentifierManager的isAdvertisingTrackingEnabled方法来查看它。
isAdvertisingTrackingEnabled
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.在执行任何广告跟踪之前,检查该属性的价值。如果值为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
}
#3
13
ASIdentifierManager is 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]广告标识符];去得到它。
#4
10
Get IDFA in 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
}
#5
5
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- 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特性强-限制-广告跟踪/
#6
4
Here's a commented helper class in Swift that will give you a nil
object 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.framework
in your target via the "Linked Frameworks and Libraries" section in the General settings tab.
只需将其添加到您的项目中(例如,在名为IDFA.swift的文件中),并通过General settings选项卡中的“链接框架和库”部分链接到目标中的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
}
#7
3
Swift 3 & 4
斯威夫特3 & 4
var IDFA = String()
if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
IDFA = ASIdentifierManager.shared().advertisingIdentifier
}
#8
1
Just to extend Amro's Swift answer, here's similar code wrapped in a method:
为了扩展Amro银行的快速回答,这里有一种方法中包含的类似代码:
import AdSupport
...
func provideIdentifierForAdvertisingIfAvailable() -> String? {
if ASIdentifierManager.sharedManager().advertisingTrackingEnabled {
return ASIdentifierManager.sharedManager().advertisingIdentifier?.UUIDString ?? nil
} else {
return nil
}
}