iOS系统归因分两种,iOS14.3以前使用iad.framework 以后使用 AdSupport和AdServices
这些framework
注意在target -> build phases -> Link binary with libraries的status中 选择optional
直接上代码 iOS14.3以下
@objc private func requestIADsDetail() {
if #available(iOS 14.3, *) { return }
if self.retryAdsCount <= 0 { return }
self.retryAdsCount -= 1
//原接入 IAD的广告投放用户接入内容为else内的内容 IOS4.0+ 如果用户接入了新的14.3+的判断会进入最下面的获取归因信息
var attribution_enable = true
if #available(iOS 14.0, *) {
let status = ATTrackingManager.trackingAuthorizationStatus
attribution_enable = status == .notDetermined || status == .authorized
if #available(iOS 14.5, *) {
attribution_enable = status == .authorized
}
}
if attribution_enable {
//获取详细归因
ADClient.shared().requestAttributionDetails({ attributionDetails, error in
print("详细归因 \(attributionDetails)")
if #available(iOS 12.0, *) {
if let error = error as? ADClientError, error != nil {
//5s内三次重试
self.perform(#selector(self.requestIADsDetail), with: nil, afterDelay: 5)
} else if error == nil {
if let att = attributionDetails?.values.first as? [String: Any] {
self.reportIAD(info: att)
}
}
}
})
}
}
/// 数据上传服务器
private func reportIAD(info: [String: Any]) {
API.reportIadPromptionInfo(iadOrgName: info["iad-org-name"] as? String,
iadOrgId: Int64(info["iad-org-id"] as? String ?? "") ?? 0,
iadCampaignId: Int64(info["iad-campaign-id"] as? String ?? "") ?? 0,
iadCampaignName: info["iad-campaign-name"] as? String,
iadPurchaseDate: Int64(XDStartupManager.getDateFromTime(time: (info["iad-purchase-date"] as? String ?? "")).timeIntervalSince1970),
iadConversionDate: Int64(XDStartupManager.getDateFromTime(time: (info["iad-conversion-date"] as? String ?? "")).timeIntervalSince1970),
iadConversionType: info["iad-conversion-type"] as? String,
iadClickDate: Int64(XDStartupManager.getDateFromTime(time: (info["iad-click-date"] as? String ?? "")).timeIntervalSince1970),
iadAdGroupId: Int64(info["iad-adgroup-id"] as? String ?? "") ?? 0,
iadCountryOrRegion: info["iad-country-or-region"] as? String,
iadKeyword: info["iad-keyword-id"] as? String,
iadKeywordMatchType: info["iad-keyword-matchtype"] as? String,
iadAdId: Int64(info["iad-ad-id"] as? String ?? "") ?? 0,
iadCreativesetId: Int64(info["iad-creativeset-id"] as? String ?? "") ?? 0,
iadCreativesetName: info["iad-creativeset-name"] as? String,
iadLineitemId: Int64(info["iad-lineitem-id"] as? String ?? "") ?? 0,
iadAdGroupName: info["iad-adgroup-name"] as? String,
iadLineitemName: info["iad-lineitem-name"] as? String) {
success, msg in
print(msg)
}
}
/// 解析特殊数据格式 eg: 2022-02-21T11:15:22Z
static func getDateFromTime(time:String) ->Date{
let dateformatter = DateFormatter()
dateformatter.locale = Locale(identifier: "en_US_POSIX")
dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateformatter.date(from: time)!
}
这里的坑点是要注意数据解析,特别是转时间戳这里
第二是 如果归因数据获取失败,这里加了一个重试策略
iOS14.3以后就简单了
if #available(iOS 14.3, *) {
do {
token = try AAAttribution.attributionToken()
print("ads、iad和asa token = \(token)")
} catch {
print("ads catch")
}
}
直接把token发到server就行了,当然server其实最终也是类似解析出来归因数据包,然后做记录