cake_wallet/cw_mweb/ios/Classes/CwMwebPlugin.swift

72 lines
2.7 KiB
Swift
Raw Normal View History

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)")
2024-05-24 18:00:43 +00:00
let dataDir = args?["dataDir"]
var error: NSError?
2024-07-17 16:27:31 +00:00
if dataDir == "stop" && CwMwebPlugin.server != nil {
print("Stopping server")
CwMwebPlugin.server?.stop()
CwMwebPlugin.server = nil
result(0)
return
}
2024-05-24 18:00:43 +00:00
if CwMwebPlugin.server == nil {
CwMwebPlugin.server = MwebdNewServer("", dataDir, "", &error)
if let server = CwMwebPlugin.server {
do {
2024-07-17 16:27:31 +00:00
print("starting server \(CwMwebPlugin.port)")
2024-05-24 18:00:43 +00:00
try server.start(0, ret0_: &CwMwebPlugin.port)
result(CwMwebPlugin.port)
} catch let startError as NSError {
print("Server Start Error: \(startError.localizedDescription)")
2024-05-24 18:00:43 +00:00
result(FlutterError(code: "Server Start Error", message: startError.localizedDescription, details: nil))
}
} else if let error = error {
print("Server Creation Error: \(error.localizedDescription)")
2024-05-24 18:00:43 +00:00
result(FlutterError(code: "Server Creation Error", message: error.localizedDescription, details: nil))
} else {
print("Unknown Error: Failed to create server")
2024-05-24 18:00:43 +00:00
result(FlutterError(code: "Unknown Error", message: "Failed to create server", details: nil))
}
} else {
print("Server already running on port: \(CwMwebPlugin.port)")
2024-05-24 18:00:43 +00:00
// 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 {
print("Stopping and cleaning up server")
2024-06-08 12:26:48 +00:00
// Perform cleanup tasks
CwMwebPlugin.server?.stop()
CwMwebPlugin.server = nil
}
2024-04-20 23:20:52 +00:00
}