The Webview Univeral Flutter package for Appeto SDK.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

287 lignes
11 KiB

  1. import Cocoa
  2. import FlutterMacOS
  3. import WebKit
  4. private var viewId: Int64 = 0
  5. public class WebviewUniversalPlugin: NSObject, FlutterPlugin {
  6. private let methodChannel: FlutterMethodChannel
  7. private var webviews: [Int64: WebviewWindowController] = [:]
  8. public init(methodChannel: FlutterMethodChannel) {
  9. self.methodChannel = methodChannel
  10. super.init()
  11. }
  12. public static func register(with registrar: FlutterPluginRegistrar) {
  13. let channel = FlutterMethodChannel(name: "webview_window", binaryMessenger: registrar.messenger)
  14. let instance = WebviewUniversalPlugin(methodChannel: channel)
  15. registrar.addMethodCallDelegate(instance, channel: channel)
  16. ClientMessageChannelPlugin.register(with: registrar)
  17. }
  18. public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  19. switch call.method {
  20. case "create":
  21. guard let argument = call.arguments as? [String: Any?] else {
  22. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  23. return
  24. }
  25. let width = argument["windowWidth"] as? Int ?? 1280
  26. let height = argument["windowHeight"] as? Int ?? 720
  27. let title = argument["title"] as? String ?? ""
  28. let titleBarHeight = argument["titleBarHeight"] as? Int ?? 50
  29. let titleBarTopPadding = argument["titleBarTopPadding"] as? Int ?? 0
  30. let controller = WebviewWindowController(
  31. viewId: viewId, methodChannel: methodChannel,
  32. width: width, height: height, title: title,
  33. titleBarHeight: titleBarHeight, titleBarTopPadding: titleBarTopPadding
  34. )
  35. controller.webviewPlugin = self
  36. webviews[viewId] = controller
  37. controller.showWindow(nil)
  38. result(viewId)
  39. viewId += 1
  40. break
  41. case "launch":
  42. guard let argument = call.arguments as? [String: Any?] else {
  43. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  44. return
  45. }
  46. guard let viewId = argument["viewId"] as? Int64 else {
  47. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  48. return
  49. }
  50. guard let url = argument["url"] as? String else {
  51. result(FlutterError(code: "0", message: "param url not found", details: nil))
  52. return
  53. }
  54. guard let parsedUrl = URL(string: url) else {
  55. result(FlutterError(code: "0", message: "failed to parse \(url)", details: nil))
  56. return
  57. }
  58. guard let wc = webviews[viewId] else {
  59. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  60. return
  61. }
  62. wc.webViewController.load(url: parsedUrl)
  63. result(nil)
  64. break
  65. case "registerJavaScripInterface":
  66. guard let argument = call.arguments as? [String: Any?] else {
  67. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  68. return
  69. }
  70. guard let viewId = argument["viewId"] as? Int64 else {
  71. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  72. return
  73. }
  74. guard let name = argument["name"] as? String else {
  75. result(FlutterError(code: "0", message: "param name not found", details: nil))
  76. return
  77. }
  78. guard let wc = webviews[viewId] else {
  79. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  80. return
  81. }
  82. wc.webViewController.addJavascriptInterface(name: name)
  83. result(nil)
  84. break
  85. case "unregisterJavaScripInterface":
  86. guard let argument = call.arguments as? [String: Any?] else {
  87. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  88. return
  89. }
  90. guard let viewId = argument["viewId"] as? Int64 else {
  91. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  92. return
  93. }
  94. guard let name = argument["name"] as? String else {
  95. result(FlutterError(code: "0", message: "param name not found", details: nil))
  96. return
  97. }
  98. guard let wc = webviews[viewId] else {
  99. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  100. return
  101. }
  102. wc.webViewController.addJavascriptInterface(name: name)
  103. result(nil)
  104. break
  105. case "clearAll":
  106. WKWebsiteDataStore.default().removeData(
  107. ofTypes: [WKWebsiteDataTypeCookies, WKWebsiteDataTypeLocalStorage],
  108. modifiedSince: .distantPast,
  109. completionHandler: {
  110. result(nil)
  111. })
  112. break
  113. case "setBrightness":
  114. guard let argument = call.arguments as? [String: Any?] else {
  115. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  116. return
  117. }
  118. guard let viewId = argument["viewId"] as? Int64 else {
  119. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  120. return
  121. }
  122. guard let wc = webviews[viewId] else {
  123. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  124. return
  125. }
  126. let brightness = argument["brightness"] as? Int ?? -1
  127. wc.setAppearance(brightness: brightness)
  128. result(nil)
  129. break
  130. case "addScriptToExecuteOnDocumentCreated":
  131. guard let argument = call.arguments as? [String: Any?] else {
  132. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  133. return
  134. }
  135. guard let viewId = argument["viewId"] as? Int64 else {
  136. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  137. return
  138. }
  139. guard let wc = webviews[viewId] else {
  140. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  141. return
  142. }
  143. guard let javaScript = argument["javaScript"] as? String else {
  144. result(FlutterError(code: "0", message: "param javaScript not found", details: nil))
  145. return
  146. }
  147. wc.webViewController.addScriptToExecuteOnDocumentCreated(javaScript: javaScript)
  148. result(nil)
  149. break
  150. case "setApplicationNameForUserAgent":
  151. guard let argument = call.arguments as? [String: Any?] else {
  152. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  153. return
  154. }
  155. guard let viewId = argument["viewId"] as? Int64 else {
  156. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  157. return
  158. }
  159. guard let wc = webviews[viewId] else {
  160. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  161. return
  162. }
  163. guard let applicationName = argument["applicationName"] as? String else {
  164. result(FlutterError(code: "0", message: "param applicationName not found", details: nil))
  165. return
  166. }
  167. wc.webViewController.setApplicationNameForUserAgent(applicationName: applicationName)
  168. result(nil)
  169. break
  170. case "back":
  171. guard let argument = call.arguments as? [String: Any?] else {
  172. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  173. return
  174. }
  175. guard let viewId = argument["viewId"] as? Int64 else {
  176. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  177. return
  178. }
  179. guard let wc = webviews[viewId] else {
  180. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  181. return
  182. }
  183. wc.webViewController.goBack()
  184. break
  185. case "forward":
  186. guard let argument = call.arguments as? [String: Any?] else {
  187. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  188. return
  189. }
  190. guard let viewId = argument["viewId"] as? Int64 else {
  191. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  192. return
  193. }
  194. guard let wc = webviews[viewId] else {
  195. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  196. return
  197. }
  198. wc.webViewController.goForward()
  199. break
  200. case "reload":
  201. guard let argument = call.arguments as? [String: Any?] else {
  202. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  203. return
  204. }
  205. guard let viewId = argument["viewId"] as? Int64 else {
  206. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  207. return
  208. }
  209. guard let wc = webviews[viewId] else {
  210. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  211. return
  212. }
  213. wc.webViewController.reload()
  214. break
  215. case "stop":
  216. guard let argument = call.arguments as? [String: Any?] else {
  217. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  218. return
  219. }
  220. guard let viewId = argument["viewId"] as? Int64 else {
  221. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  222. return
  223. }
  224. guard let wc = webviews[viewId] else {
  225. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  226. return
  227. }
  228. wc.webViewController.stopLoading()
  229. break
  230. case "close":
  231. guard let argument = call.arguments as? [String: Any?] else {
  232. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  233. return
  234. }
  235. guard let viewId = argument["viewId"] as? Int64 else {
  236. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  237. return
  238. }
  239. guard let wc = webviews[viewId] else {
  240. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  241. return
  242. }
  243. wc.close()
  244. break
  245. case "evaluateJavaScript":
  246. guard let argument = call.arguments as? [String: Any?] else {
  247. result(FlutterError(code: "0", message: "arg is not map", details: nil))
  248. return
  249. }
  250. guard let viewId = argument["viewId"] as? Int64 else {
  251. result(FlutterError(code: "0", message: "param viewId not found", details: nil))
  252. return
  253. }
  254. guard let wc = webviews[viewId] else {
  255. result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
  256. return
  257. }
  258. guard let js = argument["javaScriptString"] as? String else {
  259. result(FlutterError(code: "0", message: "param javaScriptString not found", details: nil))
  260. return
  261. }
  262. wc.webViewController.evaluateJavaScript(javaScriptString: js, completer: result)
  263. break
  264. default:
  265. result(FlutterMethodNotImplemented)
  266. }
  267. }
  268. func onWebviewWindowClose(viewId: Int64, wc: WebviewWindowController) {
  269. wc.destroy()
  270. let wc = webviews.removeValue(forKey: viewId)
  271. wc?.window?.windowController = nil
  272. wc?.window = nil
  273. }
  274. }