2024-04-20 23:20:52 +00:00
|
|
|
import Flutter
|
|
|
|
import UIKit
|
2024-05-24 18:00:43 +00:00
|
|
|
import Mwebd
|
2024-04-20 23:20:52 +00:00
|
|
|
|
|
|
|
public class CwMwebPlugin: NSObject, FlutterPlugin {
|
|
|
|
public static func register(with registrar: FlutterPluginRegistrar) {
|
|
|
|
let channel = FlutterMethodChannel(name: "cw_mweb", binaryMessenger: registrar.messenger())
|
|
|
|
let instance = CwMwebPlugin()
|
|
|
|
registrar.addMethodCallDelegate(instance, channel: channel)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:00:43 +00:00
|
|
|
private static var server: MwebdServer?
|
|
|
|
private static var port: Int = 0
|
|
|
|
|
2024-04-20 23:20:52 +00:00
|
|
|
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
2024-05-24 18:00:43 +00:00
|
|
|
switch call.method {
|
|
|
|
case "getPlatformVersion":
|
|
|
|
result("iOS " + UIDevice.current.systemVersion)
|
|
|
|
case "start":
|
|
|
|
let args = call.arguments as? [String: String]
|
|
|
|
print("args: \(args)")
|
|
|
|
let dataDir = args?["dataDir"]
|
|
|
|
var error: NSError?
|
|
|
|
|
|
|
|
if CwMwebPlugin.server == nil {
|
|
|
|
CwMwebPlugin.server = MwebdNewServer("", dataDir, "", &error)
|
|
|
|
|
|
|
|
if let server = CwMwebPlugin.server {
|
|
|
|
do {
|
|
|
|
print("starting server \(CwMwebPlugin.port)")
|
|
|
|
try server.start(0, ret0_: &CwMwebPlugin.port)
|
|
|
|
result(CwMwebPlugin.port)
|
|
|
|
} catch let startError as NSError {
|
|
|
|
result(FlutterError(code: "Server Start Error", message: startError.localizedDescription, details: nil))
|
|
|
|
}
|
|
|
|
} else if let error = error {
|
|
|
|
result(FlutterError(code: "Server Creation Error", message: error.localizedDescription, details: nil))
|
|
|
|
} else {
|
|
|
|
result(FlutterError(code: "Unknown Error", message: "Failed to create server", details: nil))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// result(FlutterError(code: "Server Already Running", message: "The server is already running", details: nil))
|
|
|
|
result(CwMwebPlugin.port)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// result(0)
|
2024-04-20 23:20:52 +00:00
|
|
|
default:
|
|
|
|
result(FlutterMethodNotImplemented)
|
|
|
|
}
|
|
|
|
}
|
2024-06-08 12:26:48 +00:00
|
|
|
|
|
|
|
deinit {
|
|
|
|
// Perform cleanup tasks
|
|
|
|
CwMwebPlugin.server?.stop()
|
|
|
|
CwMwebPlugin.server = nil
|
|
|
|
}
|
2024-04-20 23:20:52 +00:00
|
|
|
}
|