Merge pull request #1936 from michalsrutek/feature/swift-code-improvements

Swift code improvements
This commit is contained in:
Lorenzo Pichilli 2023-12-23 14:10:33 +01:00 committed by GitHub
commit bccd7535ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
79 changed files with 183 additions and 180 deletions

View File

@ -9,8 +9,9 @@ import Foundation
public class CredentialDatabase: ChannelDelegate { public class CredentialDatabase: ChannelDelegate {
static let METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_credential_database" static let METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_credential_database"
var plugin: SwiftFlutterPlugin? static let credentialStore = URLCredentialStorage.shared
static var credentialStore = URLCredentialStorage.shared
private var plugin: SwiftFlutterPlugin?
init(plugin: SwiftFlutterPlugin) { init(plugin: SwiftFlutterPlugin) {
super.init(channel: FlutterMethodChannel(name: CredentialDatabase.METHOD_CHANNEL_NAME, binaryMessenger: plugin.registrar!.messenger())) super.init(channel: FlutterMethodChannel(name: CredentialDatabase.METHOD_CHANNEL_NAME, binaryMessenger: plugin.registrar!.messenger()))
@ -44,7 +45,7 @@ public class CredentialDatabase: ChannelDelegate {
let host = arguments!["host"] as! String let host = arguments!["host"] as! String
let urlProtocol = arguments!["protocol"] as? String let urlProtocol = arguments!["protocol"] as? String
let urlPort = arguments!["port"] as? Int ?? 0 let urlPort = arguments!["port"] as? Int ?? 0
var realm = arguments!["realm"] as? String; var realm = arguments!["realm"] as? String
if let r = realm, r.isEmpty { if let r = realm, r.isEmpty {
realm = nil realm = nil
} }
@ -64,7 +65,7 @@ public class CredentialDatabase: ChannelDelegate {
let host = arguments!["host"] as! String let host = arguments!["host"] as! String
let urlProtocol = arguments!["protocol"] as? String let urlProtocol = arguments!["protocol"] as? String
let urlPort = arguments!["port"] as? Int ?? 0 let urlPort = arguments!["port"] as? Int ?? 0
var realm = arguments!["realm"] as? String; var realm = arguments!["realm"] as? String
if let r = realm, r.isEmpty { if let r = realm, r.isEmpty {
realm = nil realm = nil
} }
@ -80,14 +81,14 @@ public class CredentialDatabase: ChannelDelegate {
let host = arguments!["host"] as! String let host = arguments!["host"] as! String
let urlProtocol = arguments!["protocol"] as? String let urlProtocol = arguments!["protocol"] as? String
let urlPort = arguments!["port"] as? Int ?? 0 let urlPort = arguments!["port"] as? Int ?? 0
var realm = arguments!["realm"] as? String; var realm = arguments!["realm"] as? String
if let r = realm, r.isEmpty { if let r = realm, r.isEmpty {
realm = nil realm = nil
} }
let username = arguments!["username"] as! String let username = arguments!["username"] as! String
let password = arguments!["password"] as! String let password = arguments!["password"] as! String
var credential: URLCredential? = nil; var credential: URLCredential? = nil
var protectionSpaceCredential: URLProtectionSpace? = nil var protectionSpaceCredential: URLProtectionSpace? = nil
for (protectionSpace, credentials) in CredentialDatabase.credentialStore.allCredentials { for (protectionSpace, credentials) in CredentialDatabase.credentialStore.allCredentials {
@ -116,12 +117,12 @@ public class CredentialDatabase: ChannelDelegate {
let host = arguments!["host"] as! String let host = arguments!["host"] as! String
let urlProtocol = arguments!["protocol"] as? String let urlProtocol = arguments!["protocol"] as? String
let urlPort = arguments!["port"] as? Int ?? 0 let urlPort = arguments!["port"] as? Int ?? 0
var realm = arguments!["realm"] as? String; var realm = arguments!["realm"] as? String
if let r = realm, r.isEmpty { if let r = realm, r.isEmpty {
realm = nil realm = nil
} }
var credentialsToRemove: [URLCredential] = []; var credentialsToRemove: [URLCredential] = []
var protectionSpaceCredential: URLProtectionSpace? = nil var protectionSpaceCredential: URLProtectionSpace? = nil
for (protectionSpace, credentials) in CredentialDatabase.credentialStore.allCredentials { for (protectionSpace, credentials) in CredentialDatabase.credentialStore.allCredentials {

View File

@ -9,13 +9,13 @@ import Foundation
import Flutter import Flutter
public class FindInteractionController: NSObject, Disposable { public class FindInteractionController: NSObject, Disposable {
static let METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_find_interaction_"
static var METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_find_interaction_";
var plugin: SwiftFlutterPlugin?
var webView: InAppWebView? var webView: InAppWebView?
var channelDelegate: FindInteractionChannelDelegate? var channelDelegate: FindInteractionChannelDelegate?
var settings: FindInteractionSettings?
var shouldCallOnRefresh = false private var plugin: SwiftFlutterPlugin?
private var settings: FindInteractionSettings?
private var _searchText: String? = nil private var _searchText: String? = nil
var searchText: String? { var searchText: String? {

View File

@ -9,6 +9,7 @@ import Foundation
public class HeadlessInAppWebView: Disposable { public class HeadlessInAppWebView: Disposable {
static let METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_headless_inappwebview_" static let METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_headless_inappwebview_"
var id: String var id: String
var channelDelegate: HeadlessWebViewChannelDelegate? var channelDelegate: HeadlessWebViewChannelDelegate?
var flutterWebView: FlutterWebViewController? var flutterWebView: FlutterWebViewController?
@ -24,7 +25,7 @@ public class HeadlessInAppWebView : Disposable {
} }
public func onWebViewCreated() { public func onWebViewCreated() {
channelDelegate?.onWebViewCreated(); channelDelegate?.onWebViewCreated()
} }
public func prepare(params: NSDictionary) { public func prepare(params: NSDictionary) {

View File

@ -11,7 +11,7 @@ import WebKit
import Foundation import Foundation
public class InAppBrowserWebViewController: UIViewController, InAppBrowserDelegate, UIScrollViewDelegate, UISearchBarDelegate, Disposable { public class InAppBrowserWebViewController: UIViewController, InAppBrowserDelegate, UIScrollViewDelegate, UISearchBarDelegate, Disposable {
static var METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappbrowser_"; static let METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappbrowser_"
var closeButton: UIBarButtonItem! var closeButton: UIBarButtonItem!
var reloadButton: UIBarButtonItem! var reloadButton: UIBarButtonItem!

View File

@ -9,7 +9,7 @@ import Foundation
public class ContextMenuSettings: ISettings<NSObject> { public class ContextMenuSettings: ISettings<NSObject> {
var hideDefaultSystemContextMenuItems = false; var hideDefaultSystemContextMenuItems = false
override init(){ override init(){
super.init() super.init()

View File

@ -14,7 +14,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
WKDownloadDelegate, WKDownloadDelegate,
PullToRefreshDelegate, PullToRefreshDelegate,
Disposable { Disposable {
static var METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_" static let METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_"
var id: Any? // viewId var id: Any? // viewId
var plugin: SwiftFlutterPlugin? var plugin: SwiftFlutterPlugin?
@ -32,8 +32,8 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
var inFullscreen = false var inFullscreen = false
var preventGestureDelay = false var preventGestureDelay = false
static var sslCertificatesMap: [String: SslCertificate] = [:] // [URL host name : SslCertificate] private static var sslCertificatesMap: [String: SslCertificate] = [:] // [URL host name : SslCertificate]
static var credentialsProposed: [URLCredential] = [] private static var credentialsProposed: [URLCredential] = []
var lastScrollX: CGFloat = 0 var lastScrollX: CGFloat = 0
var lastScrollY: CGFloat = 0 var lastScrollY: CGFloat = 0
@ -103,14 +103,14 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
set { set {
super.frame = newValue super.frame = newValue
self.scrollView.contentInset = UIEdgeInsets.zero; self.scrollView.contentInset = .zero
if #available(iOS 11, *) { if #available(iOS 11, *) {
// Above iOS 11, adjust contentInset to compensate the adjustedContentInset so the sum will // Above iOS 11, adjust contentInset to compensate the adjustedContentInset so the sum will
// always be 0. // always be 0.
if (scrollView.adjustedContentInset != UIEdgeInsets.zero) { if (scrollView.adjustedContentInset != UIEdgeInsets.zero) {
let insetToAdjust = self.scrollView.adjustedContentInset; let insetToAdjust = self.scrollView.adjustedContentInset
scrollView.contentInset = UIEdgeInsets(top: -insetToAdjust.top, left: -insetToAdjust.left, scrollView.contentInset = UIEdgeInsets(top: -insetToAdjust.top, left: -insetToAdjust.left,
bottom: -insetToAdjust.bottom, right: -insetToAdjust.right); bottom: -insetToAdjust.bottom, right: -insetToAdjust.right)
} }
} }
} }
@ -2190,7 +2190,6 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
} }
struct IdentityAndTrust { struct IdentityAndTrust {
var identityRef: SecIdentity var identityRef: SecIdentity
var trust: SecTrust var trust: SecTrust
var certArray: AnyObject var certArray: AnyObject
@ -2200,7 +2199,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
var identityAndTrust: IdentityAndTrust? var identityAndTrust: IdentityAndTrust?
var securityError: OSStatus = errSecSuccess var securityError: OSStatus = errSecSuccess
var importResult: CFArray? = nil var importResult: CFArray?
securityError = SecPKCS12Import( securityError = SecPKCS12Import(
PKCS12Data as NSData, PKCS12Data as NSData,
[kSecImportExportPassphrase as String: password] as NSDictionary, [kSecImportExportPassphrase as String: password] as NSDictionary,
@ -2208,19 +2207,19 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
) )
if securityError == errSecSuccess { if securityError == errSecSuccess {
let certItems:CFArray = importResult! as CFArray; let certItems: CFArray = importResult! as CFArray
let certItemsArray: Array = certItems as Array let certItemsArray: Array = certItems as Array
let dict:AnyObject? = certItemsArray.first; let dict: AnyObject? = certItemsArray.first
if let certEntry: Dictionary = dict as? Dictionary<String, AnyObject> { if let certEntry: Dictionary = dict as? Dictionary<String, AnyObject> {
// grab the identity // grab the identity
let identityPointer:AnyObject? = certEntry["identity"]; let identityPointer: AnyObject? = certEntry["identity"]
let secIdentityRef:SecIdentity = (identityPointer as! SecIdentity?)!; let secIdentityRef:SecIdentity = (identityPointer as! SecIdentity?)!
// grab the trust // grab the trust
let trustPointer:AnyObject? = certEntry["trust"]; let trustPointer: AnyObject? = certEntry["trust"]
let trustRef:SecTrust = trustPointer as! SecTrust; let trustRef:SecTrust = trustPointer as! SecTrust
// grab the cert // grab the cert
let chainPointer:AnyObject? = certEntry["chain"]; let chainPointer: AnyObject? = certEntry["chain"]
identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certArray: chainPointer!); identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certArray: chainPointer!)
} }
} else { } else {
print("Security Error: " + securityError.description) print("Security Error: " + securityError.description)
@ -2228,7 +2227,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
print(SecCopyErrorMessageString(securityError, nil) ?? "") print(SecCopyErrorMessageString(securityError, nil) ?? "")
} }
} }
return identityAndTrust; return identityAndTrust
} }
func createAlertDialog(message: String?, responseMessage: String?, confirmButtonTitle: String?, completionHandler: @escaping () -> Void) { func createAlertDialog(message: String?, responseMessage: String?, confirmButtonTitle: String?, completionHandler: @escaping () -> Void) {
@ -2239,7 +2238,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
alertController.addAction(UIAlertAction(title: okButton, style: UIAlertAction.Style.default) { alertController.addAction(UIAlertAction(title: okButton, style: UIAlertAction.Style.default) {
_ in completionHandler()} _ in completionHandler()}
); )
guard let presentingViewController = inAppBrowserDelegate != nil ? inAppBrowserDelegate as? InAppBrowserWebViewController : window?.rootViewController else { guard let presentingViewController = inAppBrowserDelegate != nil ? inAppBrowserDelegate as? InAppBrowserWebViewController : window?.rootViewController else {
completionHandler() completionHandler()
@ -2463,16 +2462,16 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
let disableHorizontalScroll = settings?.disableHorizontalScroll ?? false let disableHorizontalScroll = settings?.disableHorizontalScroll ?? false
if startedByUser { if startedByUser {
if disableVerticalScroll && disableHorizontalScroll { if disableVerticalScroll && disableHorizontalScroll {
scrollView.contentOffset = CGPoint(x: lastScrollX, y: lastScrollY); scrollView.contentOffset = CGPoint(x: lastScrollX, y: lastScrollY)
} }
else if disableVerticalScroll { else if disableVerticalScroll {
if scrollView.contentOffset.y >= 0 || scrollView.contentOffset.y < 0 { if scrollView.contentOffset.y >= 0 || scrollView.contentOffset.y < 0 {
scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: lastScrollY); scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: lastScrollY)
} }
} }
else if disableHorizontalScroll { else if disableHorizontalScroll {
if scrollView.contentOffset.x >= 0 || scrollView.contentOffset.x < 0 { if scrollView.contentOffset.x >= 0 || scrollView.contentOffset.x < 0 {
scrollView.contentOffset = CGPoint(x: lastScrollX, y: scrollView.contentOffset.y); scrollView.contentOffset = CGPoint(x: lastScrollX, y: scrollView.contentOffset.y)
} }
} }
} }
@ -2789,24 +2788,24 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate,
switch (message.name) { switch (message.name) {
case "consoleLog": case "consoleLog":
messageLevel = 1 messageLevel = 1
break; break
case "consoleDebug": case "consoleDebug":
// on Android, console.debug is TIP // on Android, console.debug is TIP
messageLevel = 0 messageLevel = 0
break; break
case "consoleError": case "consoleError":
messageLevel = 3 messageLevel = 3
break; break
case "consoleInfo": case "consoleInfo":
// on Android, console.info is LOG // on Android, console.info is LOG
messageLevel = 1 messageLevel = 1
break; break
case "consoleWarn": case "consoleWarn":
messageLevel = 2 messageLevel = 2
break; break
default: default:
messageLevel = 1 messageLevel = 1
break; break
} }
let consoleMessage = body["message"] as? String ?? "" let consoleMessage = body["message"] as? String ?? ""

View File

@ -11,8 +11,9 @@ import WebKit
@available(iOS 11.0, *) @available(iOS 11.0, *)
public class MyCookieManager: ChannelDelegate { public class MyCookieManager: ChannelDelegate {
static let METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_cookiemanager" static let METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_cookiemanager"
var plugin: SwiftFlutterPlugin? static let httpCookieStore = WKWebsiteDataStore.default().httpCookieStore
static var httpCookieStore = WKWebsiteDataStore.default().httpCookieStore
private var plugin: SwiftFlutterPlugin?
init(plugin: SwiftFlutterPlugin) { init(plugin: SwiftFlutterPlugin) {
super.init(channel: FlutterMethodChannel(name: MyCookieManager.METHOD_CHANNEL_NAME, binaryMessenger: plugin.registrar!.messenger())) super.init(channel: FlutterMethodChannel(name: MyCookieManager.METHOD_CHANNEL_NAME, binaryMessenger: plugin.registrar!.messenger()))
@ -64,13 +65,13 @@ public class MyCookieManager: ChannelDelegate {
let path = arguments!["path"] as! String let path = arguments!["path"] as! String
let domain = arguments!["domain"] as? String let domain = arguments!["domain"] as? String
MyCookieManager.deleteCookie(url: url, name: name, path: path, domain: domain, result: result) MyCookieManager.deleteCookie(url: url, name: name, path: path, domain: domain, result: result)
break; break
case "deleteCookies": case "deleteCookies":
let url = arguments!["url"] as! String let url = arguments!["url"] as! String
let path = arguments!["path"] as! String let path = arguments!["path"] as! String
let domain = arguments!["domain"] as? String let domain = arguments!["domain"] as? String
MyCookieManager.deleteCookies(url: url, path: path, domain: domain, result: result) MyCookieManager.deleteCookies(url: url, path: path, domain: domain, result: result)
break; break
case "deleteAllCookies": case "deleteAllCookies":
MyCookieManager.deleteAllCookies(result: result) MyCookieManager.deleteAllCookies(result: result)
break break

View File

@ -11,8 +11,9 @@ import WebKit
@available(iOS 9.0, *) @available(iOS 9.0, *)
public class MyWebStorageManager: ChannelDelegate { public class MyWebStorageManager: ChannelDelegate {
static let METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_webstoragemanager" static let METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_webstoragemanager"
var plugin: SwiftFlutterPlugin? static let websiteDataStore = WKWebsiteDataStore.default()
static var websiteDataStore = WKWebsiteDataStore.default()
private var plugin: SwiftFlutterPlugin?
init(plugin: SwiftFlutterPlugin) { init(plugin: SwiftFlutterPlugin) {
super.init(channel: FlutterMethodChannel(name: MyWebStorageManager.METHOD_CHANNEL_NAME, binaryMessenger: plugin.registrar!.messenger())) super.init(channel: FlutterMethodChannel(name: MyWebStorageManager.METHOD_CHANNEL_NAME, binaryMessenger: plugin.registrar!.messenger()))

View File

@ -9,7 +9,8 @@ import Foundation
import Flutter import Flutter
public class PullToRefreshControl: UIRefreshControl, Disposable { public class PullToRefreshControl: UIRefreshControl, Disposable {
static var METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_pull_to_refresh_"; static let METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_pull_to_refresh_"
var plugin: SwiftFlutterPlugin? var plugin: SwiftFlutterPlugin?
var channelDelegate: PullToRefreshChannelDelegate? var channelDelegate: PullToRefreshChannelDelegate?
var settings: PullToRefreshSettings? var settings: PullToRefreshSettings?

View File

@ -1571,7 +1571,6 @@ public class InAppWebView: WKWebView, WKUIDelegate,
} }
struct IdentityAndTrust { struct IdentityAndTrust {
var identityRef: SecIdentity var identityRef: SecIdentity
var trust: SecTrust var trust: SecTrust
var certArray: AnyObject var certArray: AnyObject
@ -1594,14 +1593,14 @@ public class InAppWebView: WKWebView, WKUIDelegate,
let dict: AnyObject? = certItemsArray.first; let dict: AnyObject? = certItemsArray.first;
if let certEntry: Dictionary = dict as? Dictionary<String, AnyObject> { if let certEntry: Dictionary = dict as? Dictionary<String, AnyObject> {
// grab the identity // grab the identity
let identityPointer:AnyObject? = certEntry["identity"]; let identityPointer: AnyObject? = certEntry["identity"]
let secIdentityRef:SecIdentity = (identityPointer as! SecIdentity?)!; let secIdentityRef:SecIdentity = (identityPointer as! SecIdentity?)!
// grab the trust // grab the trust
let trustPointer:AnyObject? = certEntry["trust"]; let trustPointer: AnyObject? = certEntry["trust"]
let trustRef:SecTrust = trustPointer as! SecTrust; let trustRef:SecTrust = trustPointer as! SecTrust
// grab the cert // grab the cert
let chainPointer:AnyObject? = certEntry["chain"]; let chainPointer: AnyObject? = certEntry["chain"]
identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certArray: chainPointer!); identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certArray: chainPointer!)
} }
} else { } else {
print("Security Error: " + securityError.description) print("Security Error: " + securityError.description)