From 7c1cd442bbd1a5a9f41d435f00dfc65bc974eb48 Mon Sep 17 00:00:00 2001 From: Michal Srutek Date: Wed, 20 Dec 2023 15:45:58 +0100 Subject: [PATCH] Swift code improvements --- .../ios/Classes/CredentialDatabase.swift | 17 ++++++------ .../FindInteractionChannelDelegate.swift | 2 +- .../FindInteractionController.swift | 12 ++++----- .../HeadlessInAppWebView.swift | 5 ++-- .../InAppBrowserWebViewController.swift | 2 +- .../InAppWebView/ContextMenuSettings.swift | 4 +-- .../Classes/InAppWebView/InAppWebView.swift | 26 +++++++++---------- .../ios/Classes/MyCookieManager.swift | 9 ++++--- .../ios/Classes/MyWebStorageManager.swift | 5 ++-- .../PullToRefresh/PullToRefreshControl.swift | 5 ++-- 10 files changed, 46 insertions(+), 41 deletions(-) diff --git a/flutter_inappwebview_ios/ios/Classes/CredentialDatabase.swift b/flutter_inappwebview_ios/ios/Classes/CredentialDatabase.swift index 8a48275f..bc660244 100755 --- a/flutter_inappwebview_ios/ios/Classes/CredentialDatabase.swift +++ b/flutter_inappwebview_ios/ios/Classes/CredentialDatabase.swift @@ -9,8 +9,9 @@ import Foundation public class CredentialDatabase: ChannelDelegate { static let METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_credential_database" - var plugin: SwiftFlutterPlugin? - static var credentialStore = URLCredentialStorage.shared + static let credentialStore = URLCredentialStorage.shared + + private var plugin: SwiftFlutterPlugin? init(plugin: SwiftFlutterPlugin) { 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 urlProtocol = arguments!["protocol"] as? String 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 { realm = nil } @@ -64,7 +65,7 @@ public class CredentialDatabase: ChannelDelegate { let host = arguments!["host"] as! String let urlProtocol = arguments!["protocol"] as? String 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 { realm = nil } @@ -80,14 +81,14 @@ public class CredentialDatabase: ChannelDelegate { let host = arguments!["host"] as! String let urlProtocol = arguments!["protocol"] as? String 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 { realm = nil } let username = arguments!["username"] as! String let password = arguments!["password"] as! String - var credential: URLCredential? = nil; + var credential: URLCredential? = nil var protectionSpaceCredential: URLProtectionSpace? = nil for (protectionSpace, credentials) in CredentialDatabase.credentialStore.allCredentials { @@ -116,12 +117,12 @@ public class CredentialDatabase: ChannelDelegate { let host = arguments!["host"] as! String let urlProtocol = arguments!["protocol"] as? String 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 { realm = nil } - var credentialsToRemove: [URLCredential] = []; + var credentialsToRemove: [URLCredential] = [] var protectionSpaceCredential: URLProtectionSpace? = nil for (protectionSpace, credentials) in CredentialDatabase.credentialStore.allCredentials { diff --git a/flutter_inappwebview_ios/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift b/flutter_inappwebview_ios/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift index 950f622a..4b1a43aa 100644 --- a/flutter_inappwebview_ios/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift +++ b/flutter_inappwebview_ios/ios/Classes/FindInteraction/FindInteractionChannelDelegate.swift @@ -7,7 +7,7 @@ import Foundation -public class FindInteractionChannelDelegate : ChannelDelegate { +public class FindInteractionChannelDelegate: ChannelDelegate { private weak var findInteractionController: FindInteractionController? public init(findInteractionController: FindInteractionController, channel: FlutterMethodChannel) { diff --git a/flutter_inappwebview_ios/ios/Classes/FindInteraction/FindInteractionController.swift b/flutter_inappwebview_ios/ios/Classes/FindInteraction/FindInteractionController.swift index d3ebf169..cff9cc9e 100644 --- a/flutter_inappwebview_ios/ios/Classes/FindInteraction/FindInteractionController.swift +++ b/flutter_inappwebview_ios/ios/Classes/FindInteraction/FindInteractionController.swift @@ -8,14 +8,14 @@ import Foundation import Flutter -public class FindInteractionController : NSObject, Disposable { - - static var METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_find_interaction_"; - var plugin: SwiftFlutterPlugin? +public class FindInteractionController: NSObject, Disposable { + static let METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_find_interaction_" + var webView: InAppWebView? var channelDelegate: FindInteractionChannelDelegate? - var settings: FindInteractionSettings? - var shouldCallOnRefresh = false + + private var plugin: SwiftFlutterPlugin? + private var settings: FindInteractionSettings? private var _searchText: String? = nil var searchText: String? { diff --git a/flutter_inappwebview_ios/ios/Classes/HeadlessInAppWebView/HeadlessInAppWebView.swift b/flutter_inappwebview_ios/ios/Classes/HeadlessInAppWebView/HeadlessInAppWebView.swift index 57234cd2..5d13086d 100644 --- a/flutter_inappwebview_ios/ios/Classes/HeadlessInAppWebView/HeadlessInAppWebView.swift +++ b/flutter_inappwebview_ios/ios/Classes/HeadlessInAppWebView/HeadlessInAppWebView.swift @@ -7,8 +7,9 @@ import Foundation -public class HeadlessInAppWebView : Disposable { +public class HeadlessInAppWebView: Disposable { static let METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_headless_inappwebview_" + var id: String var channelDelegate: HeadlessWebViewChannelDelegate? var flutterWebView: FlutterWebViewController? @@ -24,7 +25,7 @@ public class HeadlessInAppWebView : Disposable { } public func onWebViewCreated() { - channelDelegate?.onWebViewCreated(); + channelDelegate?.onWebViewCreated() } public func prepare(params: NSDictionary) { diff --git a/flutter_inappwebview_ios/ios/Classes/InAppBrowser/InAppBrowserWebViewController.swift b/flutter_inappwebview_ios/ios/Classes/InAppBrowser/InAppBrowserWebViewController.swift index 3f59398b..2303b9d2 100755 --- a/flutter_inappwebview_ios/ios/Classes/InAppBrowser/InAppBrowserWebViewController.swift +++ b/flutter_inappwebview_ios/ios/Classes/InAppBrowser/InAppBrowserWebViewController.swift @@ -11,7 +11,7 @@ import WebKit import Foundation 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 reloadButton: UIBarButtonItem! diff --git a/flutter_inappwebview_ios/ios/Classes/InAppWebView/ContextMenuSettings.swift b/flutter_inappwebview_ios/ios/Classes/InAppWebView/ContextMenuSettings.swift index a1df58d9..9009e536 100644 --- a/flutter_inappwebview_ios/ios/Classes/InAppWebView/ContextMenuSettings.swift +++ b/flutter_inappwebview_ios/ios/Classes/InAppWebView/ContextMenuSettings.swift @@ -8,8 +8,8 @@ import Foundation public class ContextMenuSettings: ISettings { - - var hideDefaultSystemContextMenuItems = false; + + var hideDefaultSystemContextMenuItems = false override init(){ super.init() diff --git a/flutter_inappwebview_ios/ios/Classes/InAppWebView/InAppWebView.swift b/flutter_inappwebview_ios/ios/Classes/InAppWebView/InAppWebView.swift index c4390995..9d9f2cc6 100755 --- a/flutter_inappwebview_ios/ios/Classes/InAppWebView/InAppWebView.swift +++ b/flutter_inappwebview_ios/ios/Classes/InAppWebView/InAppWebView.swift @@ -14,7 +14,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKDownloadDelegate, PullToRefreshDelegate, 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 plugin: SwiftFlutterPlugin? @@ -26,14 +26,14 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, var settings: InAppWebViewSettings? var pullToRefreshControl: PullToRefreshControl? var findInteractionController: FindInteractionController? - var webMessageChannels: [String:WebMessageChannel] = [:] + var webMessageChannels: [String: WebMessageChannel] = [:] var webMessageListeners: [WebMessageListener] = [] var currentOriginalUrl: URL? var inFullscreen = false var preventGestureDelay = false - static var sslCertificatesMap: [String: SslCertificate] = [:] // [URL host name : SslCertificate] - static var credentialsProposed: [URLCredential] = [] + private static var sslCertificatesMap: [String: SslCertificate] = [:] // [URL host name : SslCertificate] + private static var credentialsProposed: [URLCredential] = [] var lastScrollX: CGFloat = 0 var lastScrollY: CGFloat = 0 @@ -103,14 +103,14 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, set { super.frame = newValue - self.scrollView.contentInset = UIEdgeInsets.zero; + self.scrollView.contentInset = .zero if #available(iOS 11, *) { // Above iOS 11, adjust contentInset to compensate the adjustedContentInset so the sum will // always be 0. if (scrollView.adjustedContentInset != UIEdgeInsets.zero) { - let insetToAdjust = self.scrollView.adjustedContentInset; + let insetToAdjust = self.scrollView.adjustedContentInset scrollView.contentInset = UIEdgeInsets(top: -insetToAdjust.top, left: -insetToAdjust.left, - bottom: -insetToAdjust.bottom, right: -insetToAdjust.right); + bottom: -insetToAdjust.bottom, right: -insetToAdjust.right) } } } @@ -2214,13 +2214,13 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, if let certEntry:Dictionary = dict as? Dictionary { // grab the identity let identityPointer:AnyObject? = certEntry["identity"]; - let secIdentityRef:SecIdentity = (identityPointer as! SecIdentity?)!; + let secIdentityRef:SecIdentity = (identityPointer as! SecIdentity?)! // grab the trust - let trustPointer:AnyObject? = certEntry["trust"]; - let trustRef:SecTrust = trustPointer as! SecTrust; + let trustPointer:AnyObject? = certEntry["trust"] + let trustRef:SecTrust = trustPointer as! SecTrust // grab the cert - let chainPointer:AnyObject? = certEntry["chain"]; - identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certArray: chainPointer!); + let chainPointer:AnyObject? = certEntry["chain"] + identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certArray: chainPointer!) } } else { print("Security Error: " + securityError.description) @@ -2228,7 +2228,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, print(SecCopyErrorMessageString(securityError,nil) ?? "") } } - return identityAndTrust; + return identityAndTrust } func createAlertDialog(message: String?, responseMessage: String?, confirmButtonTitle: String?, completionHandler: @escaping () -> Void) { diff --git a/flutter_inappwebview_ios/ios/Classes/MyCookieManager.swift b/flutter_inappwebview_ios/ios/Classes/MyCookieManager.swift index 5bcbb601..235d8f9f 100755 --- a/flutter_inappwebview_ios/ios/Classes/MyCookieManager.swift +++ b/flutter_inappwebview_ios/ios/Classes/MyCookieManager.swift @@ -11,8 +11,9 @@ import WebKit @available(iOS 11.0, *) public class MyCookieManager: ChannelDelegate { static let METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_cookiemanager" - var plugin: SwiftFlutterPlugin? - static var httpCookieStore = WKWebsiteDataStore.default().httpCookieStore + static let httpCookieStore = WKWebsiteDataStore.default().httpCookieStore + + private var plugin: SwiftFlutterPlugin? init(plugin: SwiftFlutterPlugin) { 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 domain = arguments!["domain"] as? String MyCookieManager.deleteCookie(url: url, name: name, path: path, domain: domain, result: result) - break; + break case "deleteCookies": let url = arguments!["url"] as! String let path = arguments!["path"] as! String let domain = arguments!["domain"] as? String MyCookieManager.deleteCookies(url: url, path: path, domain: domain, result: result) - break; + break case "deleteAllCookies": MyCookieManager.deleteAllCookies(result: result) break diff --git a/flutter_inappwebview_ios/ios/Classes/MyWebStorageManager.swift b/flutter_inappwebview_ios/ios/Classes/MyWebStorageManager.swift index 131afc48..a375fdfb 100755 --- a/flutter_inappwebview_ios/ios/Classes/MyWebStorageManager.swift +++ b/flutter_inappwebview_ios/ios/Classes/MyWebStorageManager.swift @@ -11,8 +11,9 @@ import WebKit @available(iOS 9.0, *) public class MyWebStorageManager: ChannelDelegate { static let METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappwebview_webstoragemanager" - var plugin: SwiftFlutterPlugin? - static var websiteDataStore = WKWebsiteDataStore.default() + static let websiteDataStore = WKWebsiteDataStore.default() + + private var plugin: SwiftFlutterPlugin? init(plugin: SwiftFlutterPlugin) { super.init(channel: FlutterMethodChannel(name: MyWebStorageManager.METHOD_CHANNEL_NAME, binaryMessenger: plugin.registrar!.messenger())) diff --git a/flutter_inappwebview_ios/ios/Classes/PullToRefresh/PullToRefreshControl.swift b/flutter_inappwebview_ios/ios/Classes/PullToRefresh/PullToRefreshControl.swift index 7d114475..5df23c17 100644 --- a/flutter_inappwebview_ios/ios/Classes/PullToRefresh/PullToRefreshControl.swift +++ b/flutter_inappwebview_ios/ios/Classes/PullToRefresh/PullToRefreshControl.swift @@ -8,8 +8,9 @@ import Foundation import Flutter -public class PullToRefreshControl : UIRefreshControl, Disposable { - static var METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_pull_to_refresh_"; +public class PullToRefreshControl: UIRefreshControl, Disposable { + static let METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_pull_to_refresh_" + var plugin: SwiftFlutterPlugin? var channelDelegate: PullToRefreshChannelDelegate? var settings: PullToRefreshSettings?