我想拿设备IDFA
。如何从iOS官方API获取此信息?
Answers:
首先:
#import <AdSupport/ASIdentifierManager.h>
如果您想将其作为NSString获得,请使用:
[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]
因此您的代码可能如下所示:
NSString *idfaString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
import AdSupport
然后ASIdentifierManager.sharedManager().advertisingIdentifier
获取IDFA!
首先,您必须检查用户用户是否已决定退出广告跟踪。只有他允许,您才能使用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
}
ASIdentifierManager是从运行iOS 6+的设备上获取广告标识号的官方方法。您可以使用-[[ASIdentifierManager sharedManager] advertisingIdentifier];
它。
在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
}
这是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
}
从iOS 10开始,当用户启用“限制广告跟踪”时,操作系统将发送带有新值“ 00000000-0000-0000-0000-0000-000000000000”的广告标识符。
根据这篇文章:https : //fpf.org/2016/08/02/ios-10-feature-stronger-limit-ad-tracking/
IDFA
-广告标识符
isAdvertisingTrackingEnabled -> trackingAuthorizationStatus
从iOS v14开始, Apple弃用isAdvertisingTrackingEnabled
了逻辑并将其移至AppTrackingTransparency
Framework中。现在,用户必须拥有读取权限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
}
如果通过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
}
}
只是为了扩展Amro的Swift答案,这是一个包装在方法中的类似代码:
import AdSupport
...
func provideIdentifierForAdvertisingIfAvailable() -> String? {
if ASIdentifierManager.sharedManager().advertisingTrackingEnabled {
return ASIdentifierManager.sharedManager().advertisingIdentifier?.UUIDString ?? nil
} else {
return nil
}
}
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
}
}