湖北秦九 e0163dabe3 1
2025-10-31 13:36:20 +08:00

94 lines
3.0 KiB
Swift

//
// FATool.swift
// Fableon
//
// Created by on 2025/9/15.
//
import Foundation
import UIKit
import AppTrackingTransparency
import AdSupport
#if DEBUG
func debugLog(_ msg: Any, file: String = #file, function: String = #function, line: Int = #line) {
print("\n\(Date(timeIntervalSinceNow: 8 * 60 * 60)) \(file.components(separatedBy: "/").last ?? "") \(function) \(line): \(msg)")
}
#else
func debugLog(_ msg: Any) { }
#endif
class FATool {
static var sceneDelegate: SceneDelegate?
static var windowScene: UIWindowScene?
static var keyWindow: UIWindow? {
return windowScene?.keyWindow
}
static var rootViewController: UIViewController? {
return keyWindow?.rootViewController
}
static var topViewController: UIViewController? {
var resultVC: UIViewController? = self.rootViewController
if let rootNav = resultVC as? UINavigationController {
resultVC = rootNav.topViewController
}
resultVC = self._topViewController(resultVC)
while resultVC?.presentedViewController != nil {
resultVC = self._topViewController(resultVC?.presentedViewController)
}
return resultVC
}
private static func _topViewController(_ vc: UIViewController?) -> UIViewController? {
if vc is UINavigationController {
return _topViewController((vc as? UINavigationController)?.topViewController)
} else if vc is UITabBarController {
return _topViewController((vc as? UITabBarController)?.selectedViewController)
} else {
return vc
}
}
}
extension FATool {
static func requestIDFAAuthorization(_ completion: ((String?) -> Void)? = nil) {
if FAAdjustStateManager.manager.idfaAuthorizationFinish {
completion?(ASIdentifierManager.shared().advertisingIdentifier.uuidString)
return
}
guard FANetworkMonitor.manager.isReachable == true, FAAdjustStateManager.manager.apnsAuthorizationFinish, FAAdjustStateManager.manager.isOpenApp else {
return
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
ATTrackingManager.requestTrackingAuthorization { status in
FAAdjustStateManager.manager.idfaAuthorizationFinish = true
let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
FATool.sceneDelegate?.retryHandleOpenAppMessage()
completion?(idfa)
}
}
}
///
static func openApnsSetting() {
if #available(iOS 16.0, *) {
if let url = URL(string: UIApplication.openNotificationSettingsURLString) {
UIApplication.shared.open(url)
}
} else {
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
}
}
}