2021-03-11 21:42:18 +00:00
|
|
|
//
|
2022-05-08 10:56:28 +00:00
|
|
|
// WebMessageChannelChannelDelegate.swift
|
2021-03-11 21:42:18 +00:00
|
|
|
// flutter_inappwebview
|
|
|
|
//
|
2022-05-08 10:56:28 +00:00
|
|
|
// Created by Lorenzo Pichilli on 07/05/22.
|
2021-03-11 21:42:18 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2023-12-21 07:07:57 +00:00
|
|
|
public class WebMessageChannelChannelDelegate: ChannelDelegate {
|
2022-05-08 10:56:28 +00:00
|
|
|
private weak var webMessageChannel: WebMessageChannel?
|
2021-03-11 21:42:18 +00:00
|
|
|
|
2022-05-08 10:56:28 +00:00
|
|
|
public init(webMessageChannel: WebMessageChannel, channel: FlutterMethodChannel) {
|
|
|
|
super.init(channel: channel)
|
|
|
|
self.webMessageChannel = webMessageChannel
|
2021-03-11 21:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
|
|
|
let arguments = call.arguments as? NSDictionary
|
|
|
|
|
|
|
|
switch call.method {
|
|
|
|
case "setWebMessageCallback":
|
2022-05-08 10:56:28 +00:00
|
|
|
if let _ = webMessageChannel?.webView, let ports = webMessageChannel?.ports, ports.count > 0 {
|
2021-03-11 21:42:18 +00:00
|
|
|
let index = arguments!["index"] as! Int
|
|
|
|
let port = ports[index]
|
|
|
|
do {
|
|
|
|
try port.setWebMessageCallback { (_) in
|
|
|
|
result(true)
|
|
|
|
}
|
|
|
|
} catch let error as NSError {
|
|
|
|
result(FlutterError(code: "WebMessageChannel", message: error.domain, details: nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2022-05-08 10:56:28 +00:00
|
|
|
result(true)
|
2021-03-11 21:42:18 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case "postMessage":
|
2022-05-08 10:56:28 +00:00
|
|
|
if let webView = webMessageChannel?.webView, let ports = webMessageChannel?.ports, ports.count > 0 {
|
2021-03-11 21:42:18 +00:00
|
|
|
let index = arguments!["index"] as! Int
|
|
|
|
let port = ports[index]
|
2023-12-20 10:07:07 +00:00
|
|
|
let message = WebMessage.fromMap(map: arguments!["message"] as! [String: Any?])
|
2021-03-11 21:42:18 +00:00
|
|
|
|
2023-11-14 17:39:25 +00:00
|
|
|
var ports: [WebMessagePort] = []
|
|
|
|
if let notConnectedPorts = message.ports {
|
|
|
|
for notConnectedPort in notConnectedPorts {
|
|
|
|
if let webMessageChannel = webView.webMessageChannels[notConnectedPort.webMessageChannelId] {
|
|
|
|
ports.append(webMessageChannel.ports[Int(notConnectedPort.index)])
|
2021-03-11 21:42:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-14 17:39:25 +00:00
|
|
|
message.ports = ports
|
|
|
|
|
2021-03-11 21:42:18 +00:00
|
|
|
do {
|
2023-11-14 17:39:25 +00:00
|
|
|
try port.postMessage(message: message) { (_) in
|
2021-03-11 21:42:18 +00:00
|
|
|
result(true)
|
|
|
|
}
|
|
|
|
} catch let error as NSError {
|
|
|
|
result(FlutterError(code: "WebMessageChannel", message: error.domain, details: nil))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result(true)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case "close":
|
2022-05-08 10:56:28 +00:00
|
|
|
if let _ = webMessageChannel?.webView, let ports = webMessageChannel?.ports, ports.count > 0 {
|
2021-03-11 21:42:18 +00:00
|
|
|
let index = arguments!["index"] as! Int
|
|
|
|
let port = ports[index]
|
|
|
|
do {
|
|
|
|
try port.close { (_) in
|
|
|
|
result(true)
|
|
|
|
}
|
|
|
|
} catch let error as NSError {
|
|
|
|
result(FlutterError(code: "WebMessageChannel", message: error.domain, details: nil))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result(true)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
result(FlutterMethodNotImplemented)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:39:25 +00:00
|
|
|
public func onMessage(index: Int64, message: WebMessage?) {
|
2021-03-11 21:42:18 +00:00
|
|
|
let arguments: [String:Any?] = [
|
|
|
|
"index": index,
|
2023-11-14 17:39:25 +00:00
|
|
|
"message": message?.toMap()
|
2021-03-11 21:42:18 +00:00
|
|
|
]
|
|
|
|
channel?.invokeMethod("onMessage", arguments: arguments)
|
|
|
|
}
|
|
|
|
|
2022-05-08 10:56:28 +00:00
|
|
|
public override func dispose() {
|
|
|
|
super.dispose()
|
|
|
|
webMessageChannel = nil
|
2021-03-11 21:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
2022-04-23 02:02:37 +00:00
|
|
|
dispose()
|
2021-03-11 21:42:18 +00:00
|
|
|
}
|
|
|
|
}
|