看到算你捡到宝了,直接用就行
import AdServices
import Foundation
import iAd
struct ASA {
static let shared = ASA()
private init() {}
func getToken() -> String? {
var adToken: String?
if #available(iOS 14.3, *) {
do {
adToken = try AAAttribution.attributionToken()
} catch {}
}
return adToken
}
func AppleASA() {
if #available(iOS 14.3, *) {
let adToken = getToken()
sendToken(Token: adToken ?? "") { attrData in
postData(tokenData: attrData)
print("LogAds:14.3+ Dict: \(attrData ?? [:])")
if let attrData = attrData {
var attrDataL: [String: Any] = attrData
attrDataL["type"] = "1"
}
}
} else {
ADClient.shared().requestAttributionDetails { attrData, _ in
postData(tokenData: attrData)
print("LogAds:14- Dict: \(attrData ?? [:])")
}
}
}
private func postData(tokenData: [String: Any]?) {
guard let tokenData = tokenData else {
return
}
}
private func sendToken(Token: String, completeBlock: @escaping (_ data: [String: Any]?) -> Void) {
let url = "https://api-adservices.apple.com/api/v1/"
let postData = Token.data(using: .utf8, allowLossyConversion: true)
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "POST"
request.addValue("text/plain", forHTTPHeaderField: "Content-Type")
request.httpBody = postData
let datatask = URLSession.shared.dataTask(with: request as URLRequest) { data, _, error in
var result: [String: Any]?
guard let _ = error else {
var resDic: [String: Any]?
do {
resDic = try JSONSerialization.jsonObject(with: data ?? Data(), options: []) as? [String: Any]
} catch {}
result = resDic
completeBlock(result)
return
}
print("失败")
let nulldict: [String: Any] = [:]
completeBlock(nulldict)
}
datatask.resume()
}
}
|