topon_playb/PlayBTopOn/playB/idfa.swift
2025-08-06 16:04:31 +08:00

97 lines
3.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// idfa.swift
// playbtest
//
// Created by 16 on 2024/12/31.
//
import AdSupport
import AppTrackingTransparency
import Foundation
class IDFA {
static let shared = IDFA()
/// ATT
@available(iOS 14, *)
func checkATT(completion: @escaping (String?) -> Void) {
let status = ATTrackingManager.trackingAuthorizationStatus
switch status {
case .notDetermined:
//
ATTrackingManager.requestTrackingAuthorization { newStatus in
self.handleATTStatus(newStatus, completion: completion)
}
case .authorized, .denied, .restricted:
//
handleATTStatus(status, completion: completion)
@unknown default:
completion(nil)
}
}
/// ATT
@available(iOS 14, *)
private func handleATTStatus(
_ status: ATTrackingManager.AuthorizationStatus,
completion: @escaping (String?) -> Void
) {
switch status {
case .authorized:
// IDFA
let idfa = ASIdentifierManager.shared().advertisingIdentifier
.uuidString
completion(idfa)
// starManager.shared.idfa = idfa
case .denied, .restricted:
// nil
print("用户拒绝授权或功能受限")
completion(nil)
case .notDetermined:
//
print("授权状态仍未确定")
completion(nil)
@unknown default:
completion(nil)
}
}
func getIDFAForOlderVersions(completion: @escaping (String?) -> Void) {
if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
let idfa = ASIdentifierManager.shared().advertisingIdentifier
.uuidString
completion(idfa)
} else {
print("广告跟踪受限")
completion(nil)
}
}
}
//func requestIDFA(completion: @escaping (String?) -> Void) {
// if #available(iOS 14.5, *) {
// //
// ATTrackingManager.requestTrackingAuthorization { status in
// switch status {
// case .authorized:
// // IDFA
// let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
// completion(idfa)
// case .denied, .restricted, .notDetermined:
// // IDFA
// completion(nil)
// @unknown default:
// completion(nil)
// }
// }
// } else {
// // iOS 14.4 IDFA
// if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
// let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
// completion(idfa)
// } else {
// completion(nil)
// }
// }
//}