Visual_Novel_iOS/crush/Crush/Src/Utils/Tools.swift

201 lines
6.2 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

//
// Tools.swift
// Crush
//
// Created by Leon on 2025/7/12.
//
import Foundation
import AdSupport
import AppTrackingTransparency
///
class CLTool {
static let shared = CLTool()
var appVersion: AppVersion?
static func clearTempFolder() {
let fileManager = FileManager.default
let tempFolderPath = NSTemporaryDirectory()
do {
let filePaths = try fileManager.contentsOfDirectory(atPath: tempFolderPath)
for filePath in filePaths {
// dlog("filaPath: \(filePath)")
try fileManager.removeItem(atPath: NSTemporaryDirectory() + filePath)
}
} catch let error as NSError {
print("Could not clear temp folder: \(error.debugDescription)")
}
}
static func feedbackGenerator() {
if #available(iOS 13.0, *) {
let gen = UIImpactFeedbackGenerator(style: .medium) // light
gen.prepare() //
gen.impactOccurred() //
}
}
static func syncInputTintColor() {
UITextField.appearance().tintColor = .blue
UITextView.appearance().tintColor = .blue
}
// MARK: - public
public func requestAppVersion(block: ((_ appVer: AppVersion?) -> Void)?) {
if let appVer = appVersion {
block?(appVer)
return
}
// CommonProvider.request(.appVersionGet, modelType: AppVersion.self, autoShowErrMsg: false) { [weak self] result in
// switch result {
// case let .success(model):
// self?.appVersion = model
// block?(model)
// break
// case .failure:
// break
// }
// }
}
public func findLocaliezedNameBy(code: String) -> String? {
// for per in langNameCodes {
// if per.langCode == code {
// return per.codeName
// }
// }
return nil
}
static func syncSystemLanguage() {
}
///
static func syncAppLanuage(lan: Languages, completion: ((_ result: Bool) -> Void)?) {
// MeProvider.request(.updateLanguage(lang: lan.rawValue), modelType: String.self, autoShowErrMsg: false) { result in
// switch result {
// case .success:
// completion?(true)
// break
// case .failure:
// completion?(false)
// break
// }
// }
}
/// regionCode
static func getLocalizedCountryName(localeIdentifier: String?) -> String {
guard let identi = localeIdentifier else { return "" }
var regionCode = "en"
// Code
if let currentRegionCode = Locale.current.regionCode {
regionCode = currentRegionCode
}
let locale = Locale(identifier: regionCode)
if let localisedCountryName = locale.localizedString(forRegionCode: identi) {
return localisedCountryName
} else {
dlog("无法localize国家\(identi)")
return ""
}
}
static func getLocalizedCountryNameByPreferLan(localeIdentifier: String?) -> String {
guard let identi = localeIdentifier else { return "" }
guard let lan = Languages.preferedLans.first else { return "" }
let locale = Locale(identifier: Languages.regionCode(lan: lan))
if let localisedCountryName = locale.localizedString(forRegionCode: identi) {
return localisedCountryName
} else {
dlog("Languages Prefer lan 's locale country name get failed ❌:\(identi) 采用备选方案根据Locale.current来获取")
return getLocalizedCountryName(localeIdentifier: localeIdentifier)
}
}
// MARK: - helper.
}
extension CLTool {
static func idfaSetupReport() {
if #available(iOS 14, *) {
ATTrackingManager.requestTrackingAuthorization { status in
switch status {
case .denied:
debugPrint("【IDFA】用户拒绝")
break
case .authorized:
debugPrint("【IDFA】用户允许IDFA: \(ASIdentifierManager.shared().advertisingIdentifier.uuidString)❇️")
CLTool.tryReportIDFA()
break
case .notDetermined:
debugPrint("【IDFA】用户没有选择")
default:
break
}
}
} else {
// iOS13
if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
// debugPrint(":\(ASIdentifierManager.shared().advertisingIdentifier.uuidString)")
CLTool.tryReportIDFA()
} else {
debugPrint("【IDFA】用户未打开IDFA开关")
}
}
}
fileprivate static func tryReportIDFA() {
let str = ASIdentifierManager.shared().advertisingIdentifier.uuidString
if str.contains("0000-0000-0000") || str.count == 0 {
return
}
if let isSubmmited = AppCache.fetchCache(key: CacheKey.boolIdfaSubmmited.rawValue, type: Bool.self), isSubmmited == true {
return
}
// ComplexProvider.request(.reportIDFA(str: str), modelType: String.self, autoShowErrMsg: false) { result in
// switch result {
// case let .success(model):
// dlog(model)
// AppCache.cache(key: CacheKey.boolIdfaSubmmited.rawValue, value: true)
// break
// case .failure:
// break
// }
// }
}
}
// MARK: Sandbox
extension CLTool{
static func m4aFileToBase64(fileURL: URL?) -> String? {
guard let fileURL = fileURL else {
dlog("文件 URL 为空")
return nil
}
do {
let data = try Data(contentsOf: fileURL)
let base64String = data.base64EncodedString()
return base64String
} catch {
dlog("读取或转换失败: \(error)")
return nil
}
}
}