Files
proxypin/ios/Runner/Handlers/MethodHandler.swift
2025-05-31 06:54:44 +08:00

84 lines
3.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
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.
//
// MethodHandler.swift
// Runner
//
// Created by wanghongen on 2025/5/30.
//
import Flutter
import Network
import SystemConfiguration.CaptiveNetwork
public class MethodHandler: NSObject, FlutterPlugin {
public static let name = "com.proxypin/method"
private var channel: FlutterMethodChannel?
private var currentPathMonitor: NWPathMonitor?
private var currentCompletionHandler: ((Bool) -> Void)?
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: Self.name, binaryMessenger: registrar.messenger())
let instance = MethodHandler()
registrar.addMethodCallDelegate(instance, channel: channel)
instance.channel = channel
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "requestLocalNetwork":
//
self.requestLocalNetworkAccess { isAvailable in
result(isAvailable)
}
default:
result(FlutterMethodNotImplemented)
}
}
/// Wi-Fi
/// - Parameter completion: Bool true false
func requestLocalNetworkAccess(completion: @escaping (Bool) -> Void) {
//
self.currentPathMonitor?.cancel()
self.currentPathMonitor = NWPathMonitor()
// completion 便 pathUpdateHandler
// completion
self.currentCompletionHandler = completion
self.currentPathMonitor?.pathUpdateHandler = { [weak self] path in
guard let self = self else { return }
// completionHandler
guard let completionHandler = self.currentCompletionHandler else {
//
//
self.currentPathMonitor?.cancel()
return
}
var isLocalNetworkAvailable = false
print("Network path status: \(path.status)")
if path.status == .satisfied {
if path.usesInterfaceType(.wifi) || path.usesInterfaceType(.wiredEthernet) {
isLocalNetworkAvailable = true
}
}
// ( .unsatisfied, .requiresConnection) ( cellular),
// isLocalNetworkAvailable false
// completion handler
completionHandler(isLocalNetworkAvailable)
//
self.currentPathMonitor?.cancel()
self.currentPathMonitor = nil
self.currentCompletionHandler = nil
}
//
self.currentPathMonitor?.start(queue: DispatchQueue.global())
}
}