//
//  AppDelegate.swift
//  DNSResolverSwiftDemo
//
//  Created by issuser on 2025/9/2.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        // 1. 获取 DNSResolver 单例
        let resolver = DNSResolver.share()!
        
        // 2. 设置鉴权信息（AccountId, AccessKeyId, AccesskeySecret）
        resolver.setAccountId("******",
                            andAccessKeyId: "******",
                            andAccesskeySecret: "******")
        
        // 3. 设置保活域名（自动刷新缓存）
        resolver.setKeepAliveDomains(["www.baidu.com", "www.douyin.com"])
        
        // 4. 预加载 IPv4 域名
        resolver.preloadIpv4Domains(["www.baidu.com" , "www.douyin.com", "m.taobao.com"]) {
            print("预解析完成.")
            
            // 5. 从缓存中获取 IPv4 地址（允许返回过期 IP）
            if let array = resolver.getIpv4ByCache(withDomain: "www.douyin.com", andExpiredIPEnabled: true) {
                print("array == \(array)")
            } else {
                print("缓存中无数据或解析失败")
            }
        }
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

