Files
venera/ios/Runner/AppDelegate.swift
2024-10-28 17:41:46 +08:00

85 lines
2.9 KiB
Swift

import Flutter
import UIKit
import UniformTypeIdentifiers
@main
@objc class AppDelegate: FlutterAppDelegate, UIDocumentPickerDelegate {
var flutterResult: FlutterResult?
var directoryPath: URL!
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
guard let controller = window?.rootViewController as? FlutterViewController else {
fatalError("rootViewController is not of type FlutterViewController")
}
let methodChannel = FlutterMethodChannel(name: "venera/method_channel", binaryMessenger: controller.binaryMessenger)
methodChannel.setMethodCallHandler { (call, result) in
if call.method == "getProxy" {
if let proxySettings = CFNetworkCopySystemProxySettings()?.takeUnretainedValue() as NSDictionary?,
let dict = proxySettings.object(forKey: kCFNetworkProxiesHTTPProxy) as? NSDictionary,
let host = dict.object(forKey: kCFNetworkProxiesHTTPProxy) as? String,
let port = dict.object(forKey: kCFNetworkProxiesHTTPPort) as? Int {
let proxyConfig = "\(host):\(port)"
result(proxyConfig)
} else {
result("")
}
} else if call.method == "setScreenOn" {
if let arguments = call.arguments as? Bool {
let screenOn = arguments
UIApplication.shared.isIdleTimerDisabled = screenOn
}
result(nil)
} else if call.method == "getDirectoryPath" {
self.flutterResult = result
self.getDirectoryPath()
} else if call.method == "stopAccessingSecurityScopedResource" {
self.directoryPath?.stopAccessingSecurityScopedResource()
self.directoryPath = nil
result(nil)
} else {
result(FlutterMethodNotImplemented)
}
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func getDirectoryPath() {
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [UTType.folder], asCopy: false)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
documentPicker.directoryURL = nil
documentPicker.modalPresentationStyle = .formSheet
if let rootViewController = window?.rootViewController {
rootViewController.present(documentPicker, animated: true, completion: nil)
}
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
self.directoryPath = urls.first
if self.directoryPath == nil {
flutterResult?(nil)
return
}
let success = self.directoryPath.startAccessingSecurityScopedResource()
if success {
flutterResult?(self.directoryPath.path)
} else {
flutterResult?(nil)
}
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
flutterResult?(nil)
}
}