XuqmGroup-iOSSDK/Sources/XuqmCoreSDK/ConfigFileReader.swift

39 行
1.6 KiB
Swift

import Foundation
/// Reads and decrypts the init config file from the app bundle.
/// Looks for xuqm/config.xuqm first, then falls back to any *.xuqmconfig file.
/// This is used by XuqmSDK.autoInitialize() and does NOT depend on XuqmLicenseSDK.
public enum ConfigFileReader {
public static func read() -> ConfigFileData? {
guard let url = Bundle.main.url(forResource: "config", withExtension: "xuqm", subdirectory: "xuqm"),
let encrypted = try? String(contentsOf: url, encoding: .utf8) else {
// Fallback: try any *.xuqmconfig file
if let fallbackURL = findConfigFallback(),
let encrypted = try? String(contentsOf: fallbackURL, encoding: .utf8) {
return parse(encrypted)
}
return nil
}
return parse(encrypted)
}
static func parse(_ encrypted: String) -> ConfigFileData? {
guard let json = try? ConfigFileCrypto.decrypt(encrypted),
let data = json.data(using: .utf8),
let file = try? JSONDecoder().decode(ConfigFileData.self, from: data) else {
return nil
}
return file
}
private static func findConfigFallback() -> URL? {
guard let resourceURL = Bundle.main.resourceURL else { return nil }
let xuqmDir = resourceURL.appendingPathComponent("xuqm")
guard let contents = try? FileManager.default.contentsOfDirectory(at: xuqmDir, includingPropertiesForKeys: nil) else {
return nil
}
return contents.first { $0.pathExtension.lowercased() == "xuqmconfig" }
}
}