2019-11-02 03:16:47 +00:00
|
|
|
//
|
|
|
|
// FlutterWebViewController.swift
|
2019-11-29 15:59:18 +00:00
|
|
|
// flutter_inappwebview
|
2019-11-02 03:16:47 +00:00
|
|
|
//
|
|
|
|
// Created by Lorenzo on 13/11/18.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import WebKit
|
|
|
|
|
2021-02-01 14:55:27 +00:00
|
|
|
public class FlutterWebViewController: NSObject, FlutterPlatformView {
|
2019-11-02 03:16:47 +00:00
|
|
|
|
|
|
|
private weak var registrar: FlutterPluginRegistrar?
|
|
|
|
var webView: InAppWebView?
|
2020-05-11 00:48:41 +00:00
|
|
|
var viewId: Any = 0
|
2019-11-02 03:16:47 +00:00
|
|
|
var channel: FlutterMethodChannel?
|
2019-12-16 22:58:10 +00:00
|
|
|
var myView: UIView?
|
2021-02-01 14:55:27 +00:00
|
|
|
var methodCallDelegate: InAppWebViewMethodHandler?
|
2019-11-04 00:39:23 +00:00
|
|
|
|
2021-03-26 20:04:44 +00:00
|
|
|
init(registrar: FlutterPluginRegistrar, withFrame frame: CGRect, viewIdentifier viewId: Any, params: NSDictionary) {
|
2019-11-02 03:16:47 +00:00
|
|
|
super.init()
|
2019-11-04 00:39:23 +00:00
|
|
|
|
2019-11-02 03:16:47 +00:00
|
|
|
self.registrar = registrar
|
|
|
|
self.viewId = viewId
|
|
|
|
|
2021-03-05 22:19:50 +00:00
|
|
|
channel = FlutterMethodChannel(name: "com.pichillilorenzo/flutter_inappwebview_" + String(describing: viewId),
|
|
|
|
binaryMessenger: registrar.messenger())
|
2019-12-16 22:58:10 +00:00
|
|
|
|
2020-05-11 00:48:41 +00:00
|
|
|
myView = UIView(frame: frame)
|
2021-02-03 14:17:15 +00:00
|
|
|
myView!.clipsToBounds = true
|
2020-05-11 00:48:41 +00:00
|
|
|
|
2022-04-19 23:31:14 +00:00
|
|
|
let initialSettings = params["initialSettings"] as! [String: Any?]
|
2021-03-26 20:04:44 +00:00
|
|
|
let contextMenu = params["contextMenu"] as? [String: Any]
|
|
|
|
let windowId = params["windowId"] as? Int64
|
|
|
|
let initialUserScripts = params["initialUserScripts"] as? [[String: Any]]
|
2022-04-20 01:05:46 +00:00
|
|
|
let pullToRefreshInitialSettings = params["pullToRefreshSettings"] as! [String: Any?]
|
2021-02-22 11:16:23 +00:00
|
|
|
|
|
|
|
var userScripts: [UserScript] = []
|
|
|
|
if let initialUserScripts = initialUserScripts {
|
|
|
|
for intialUserScript in initialUserScripts {
|
|
|
|
userScripts.append(UserScript.fromMap(map: intialUserScript, windowId: windowId)!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 23:31:14 +00:00
|
|
|
let settings = InAppWebViewSettings()
|
|
|
|
let _ = settings.parse(settings: initialSettings)
|
2022-04-20 01:05:46 +00:00
|
|
|
let preWebviewConfiguration = InAppWebView.preWKWebViewConfiguration(settings: settings)
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
|
|
|
|
if let wId = windowId, let webViewTransport = InAppWebView.windowWebViews[wId] {
|
|
|
|
webView = webViewTransport.webView
|
|
|
|
webView!.frame = myView!.bounds
|
|
|
|
webView!.contextMenu = contextMenu
|
|
|
|
webView!.channel = channel!
|
2021-02-22 11:16:23 +00:00
|
|
|
webView!.initialUserScripts = userScripts
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
} else {
|
2021-02-22 11:16:23 +00:00
|
|
|
webView = InAppWebView(frame: myView!.bounds,
|
|
|
|
configuration: preWebviewConfiguration,
|
|
|
|
contextMenu: contextMenu,
|
|
|
|
channel: channel!,
|
|
|
|
userScripts: userScripts)
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
}
|
2021-02-01 14:55:27 +00:00
|
|
|
|
|
|
|
methodCallDelegate = InAppWebViewMethodHandler(webView: webView!)
|
|
|
|
channel!.setMethodCallHandler(LeakAvoider(delegate: methodCallDelegate!).handle)
|
2021-03-05 22:19:50 +00:00
|
|
|
|
|
|
|
let pullToRefreshLayoutChannel = FlutterMethodChannel(name: "com.pichillilorenzo/flutter_inappwebview_pull_to_refresh_" + String(describing: viewId),
|
|
|
|
binaryMessenger: registrar.messenger())
|
2022-04-20 01:05:46 +00:00
|
|
|
let pullToRefreshSettings = PullToRefreshSettings()
|
|
|
|
let _ = pullToRefreshSettings.parse(settings: pullToRefreshInitialSettings)
|
|
|
|
let pullToRefreshControl = PullToRefreshControl(channel: pullToRefreshLayoutChannel, settings: pullToRefreshSettings)
|
2021-03-05 22:19:50 +00:00
|
|
|
webView!.pullToRefreshControl = pullToRefreshControl
|
|
|
|
pullToRefreshControl.delegate = webView!
|
|
|
|
pullToRefreshControl.prepare()
|
2019-12-16 22:58:10 +00:00
|
|
|
|
|
|
|
webView!.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
2020-05-09 02:36:07 +00:00
|
|
|
myView!.autoresizesSubviews = true
|
|
|
|
myView!.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
2019-12-16 22:58:10 +00:00
|
|
|
myView!.addSubview(webView!)
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
|
2022-04-19 23:31:14 +00:00
|
|
|
webView!.settings = settings
|
2019-11-02 03:16:47 +00:00
|
|
|
webView!.prepare()
|
2021-03-11 21:42:18 +00:00
|
|
|
webView!.windowCreated = true
|
2021-03-26 20:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func view() -> UIView {
|
|
|
|
return myView!
|
|
|
|
}
|
|
|
|
|
|
|
|
public func makeInitialLoad(params: NSDictionary) {
|
|
|
|
let windowId = params["windowId"] as? Int64
|
|
|
|
let initialUrlRequest = params["initialUrlRequest"] as? [String: Any?]
|
|
|
|
let initialFile = params["initialFile"] as? String
|
|
|
|
let initialData = params["initialData"] as? [String: String]
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
|
|
|
|
if windowId == nil {
|
|
|
|
if #available(iOS 11.0, *) {
|
|
|
|
self.webView!.configuration.userContentController.removeAllContentRuleLists()
|
2022-04-19 23:31:14 +00:00
|
|
|
if let contentBlockers = webView!.settings?.contentBlockers, contentBlockers.count > 0 {
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
do {
|
|
|
|
let jsonData = try JSONSerialization.data(withJSONObject: contentBlockers, options: [])
|
|
|
|
let blockRules = String(data: jsonData, encoding: String.Encoding.utf8)
|
|
|
|
WKContentRuleListStore.default().compileContentRuleList(
|
|
|
|
forIdentifier: "ContentBlockingRules",
|
|
|
|
encodedContentRuleList: blockRules) { (contentRuleList, error) in
|
2019-12-16 22:58:10 +00:00
|
|
|
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
if let error = error {
|
|
|
|
print(error.localizedDescription)
|
|
|
|
return
|
|
|
|
}
|
2019-12-16 22:58:10 +00:00
|
|
|
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
let configuration = self.webView!.configuration
|
|
|
|
configuration.userContentController.add(contentRuleList!)
|
2019-12-16 22:58:10 +00:00
|
|
|
|
2021-02-22 11:16:23 +00:00
|
|
|
self.load(initialUrlRequest: initialUrlRequest, initialFile: initialFile, initialData: initialData)
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
} catch {
|
|
|
|
print(error.localizedDescription)
|
2019-11-02 03:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 11:16:23 +00:00
|
|
|
load(initialUrlRequest: initialUrlRequest, initialFile: initialFile, initialData: initialData)
|
Updated onCreateWindow, onJsAlert, onJsConfirm, and onJsPrompt webview events, added onCloseWindow, onTitleChanged, onWindowFocus, and onWindowBlur webview events, added androidOnRequestFocus, androidOnReceivedIcon, androidOnReceivedTouchIconUrl, androidOnJsBeforeUnload, and androidOnReceivedLoginRequest Android-specific webview events, fix #403
2020-06-29 14:34:08 +00:00
|
|
|
}
|
|
|
|
else if let wId = windowId, let webViewTransport = InAppWebView.windowWebViews[wId] {
|
|
|
|
webView!.load(webViewTransport.request)
|
2019-11-02 03:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:04:44 +00:00
|
|
|
func load(initialUrlRequest: [String:Any?]?, initialFile: String?, initialData: [String: String]?) {
|
2021-02-12 16:14:13 +00:00
|
|
|
if let initialFile = initialFile {
|
2019-11-02 03:16:47 +00:00
|
|
|
do {
|
2021-02-22 11:16:23 +00:00
|
|
|
try webView?.loadFile(assetFilePath: initialFile)
|
2019-11-02 03:16:47 +00:00
|
|
|
}
|
|
|
|
catch let error as NSError {
|
|
|
|
dump(error)
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 11:16:23 +00:00
|
|
|
else if let initialData = initialData {
|
2021-02-12 16:14:13 +00:00
|
|
|
let data = initialData["data"]!
|
|
|
|
let mimeType = initialData["mimeType"]!
|
|
|
|
let encoding = initialData["encoding"]!
|
2021-04-02 13:18:28 +00:00
|
|
|
let baseUrl = URL(string: initialData["baseUrl"]!)!
|
|
|
|
var allowingReadAccessToURL: URL? = nil
|
2022-04-19 23:31:14 +00:00
|
|
|
if let allowingReadAccessTo = webView?.settings?.allowingReadAccessTo, baseUrl.scheme == "file" {
|
2021-04-02 13:18:28 +00:00
|
|
|
allowingReadAccessToURL = URL(string: allowingReadAccessTo)
|
|
|
|
if allowingReadAccessToURL?.scheme != "file" {
|
|
|
|
allowingReadAccessToURL = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
webView?.loadData(data: data, mimeType: mimeType, encoding: encoding, baseUrl: baseUrl, allowingReadAccessTo: allowingReadAccessToURL)
|
2019-11-02 03:16:47 +00:00
|
|
|
}
|
2021-02-22 11:16:23 +00:00
|
|
|
else if let initialUrlRequest = initialUrlRequest {
|
|
|
|
let urlRequest = URLRequest.init(fromPluginMap: initialUrlRequest)
|
2021-02-12 16:14:13 +00:00
|
|
|
var allowingReadAccessToURL: URL? = nil
|
2022-04-19 23:31:14 +00:00
|
|
|
if let allowingReadAccessTo = webView?.settings?.allowingReadAccessTo, let url = urlRequest.url, url.scheme == "file" {
|
2021-02-12 16:14:13 +00:00
|
|
|
allowingReadAccessToURL = URL(string: allowingReadAccessTo)
|
|
|
|
if allowingReadAccessToURL?.scheme != "file" {
|
|
|
|
allowingReadAccessToURL = nil
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 11:16:23 +00:00
|
|
|
webView?.loadUrl(urlRequest: urlRequest, allowingReadAccessTo: allowingReadAccessToURL)
|
2019-11-02 03:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 20:04:44 +00:00
|
|
|
|
|
|
|
func dispose() {
|
|
|
|
channel?.setMethodCallHandler(nil)
|
2022-04-23 02:02:37 +00:00
|
|
|
channel = nil
|
|
|
|
methodCallDelegate?.dispose()
|
2021-03-26 20:04:44 +00:00
|
|
|
methodCallDelegate = nil
|
|
|
|
webView?.dispose()
|
|
|
|
webView = nil
|
|
|
|
myView = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
2022-04-30 19:22:31 +00:00
|
|
|
debugPrint("FlutterWebViewController - dealloc")
|
2021-03-26 20:04:44 +00:00
|
|
|
dispose()
|
|
|
|
}
|
2019-11-02 03:16:47 +00:00
|
|
|
}
|