Files
UUVPN/iOS-SwiftUI-Code/XiaoXiong/ApplicationDelegate.swift
T
2025-01-28 12:28:03 +08:00

67 lines
2.1 KiB
Swift

import ApplicationLibrary
import Foundation
import Libbox
import Library
import Network
import UIKit
class ApplicationDelegate: NSObject, UIApplicationDelegate {
private var profileServer: ProfileServer?
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
LibboxSetup(FilePath.sharedDirectory.relativePath, FilePath.workingDirectory.relativePath, FilePath.cacheDirectory.relativePath, false)
setup()
return true
}
private func setup() {
do {
try UIProfileUpdateTask.configure()
NSLog("setup background task success")
} catch {
NSLog("setup background task error: \(error.localizedDescription)")
}
Task {
if UIDevice.current.userInterfaceIdiom == .phone {
await requestNetworkPermission()
}
await setupBackground()
}
}
private nonisolated func setupBackground() async {
if #available(iOS 16.0, *) {
do {
let profileServer = try ProfileServer()
profileServer.start()
await MainActor.run {
self.profileServer = profileServer
}
NSLog("started profile server")
} catch {
NSLog("setup profile server error: \(error.localizedDescription)")
}
}
}
private nonisolated func requestNetworkPermission() async {
if await SharedPreferences.networkPermissionRequested.get() {
return
}
if !DeviceCensorship.isChinaDevice() {
await SharedPreferences.networkPermissionRequested.set(true)
return
}
URLSession.shared.dataTask(with: URL(string: "http://captive.apple.com")!) { _, response, _ in
if let response = response as? HTTPURLResponse {
if response.statusCode == 200 {
Task {
await SharedPreferences.networkPermissionRequested.set(true)
}
}
}
}.resume()
}
}