@@ -53,61 +53,67 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
5353 super. init ( coder: aDecoder)
5454 }
5555
56- public init ( source: WKWebSource ? , credentials: WKWebViewCredentials ? = nil ) {
56+ public init ( source: WKWebSource ? , credentials: WKWebViewCredentials ? = nil , allowWebViewJsVisibilityControl : Bool = false ) {
5757 super. init ( nibName: nil , bundle: nil )
5858 self . source = source
5959 self . credentials = credentials
60+ self . allowWebViewJsVisibilityControl = allowWebViewJsVisibilityControl
6061 self . initWebview ( )
6162 }
6263
63- public init ( url: URL , credentials: WKWebViewCredentials ? = nil ) {
64+ public init ( url: URL , credentials: WKWebViewCredentials ? = nil , allowWebViewJsVisibilityControl : Bool = false ) {
6465 super. init ( nibName: nil , bundle: nil )
6566 self . source = . remote( url)
6667 self . credentials = credentials
68+ self . allowWebViewJsVisibilityControl = allowWebViewJsVisibilityControl
6769 self . initWebview ( )
6870 }
6971
70- public init ( url: URL , headers: [ String : String ] , isInspectable: Bool , credentials: WKWebViewCredentials ? = nil , preventDeeplink: Bool ) {
72+ public init ( url: URL , headers: [ String : String ] , isInspectable: Bool , credentials: WKWebViewCredentials ? = nil , preventDeeplink: Bool , allowWebViewJsVisibilityControl : Bool = false ) {
7173 super. init ( nibName: nil , bundle: nil )
7274 self . source = . remote( url)
7375 self . credentials = credentials
76+ self . allowWebViewJsVisibilityControl = allowWebViewJsVisibilityControl
7477 self . setHeaders ( headers: headers)
7578 self . setPreventDeeplink ( preventDeeplink: preventDeeplink)
7679 self . initWebview ( isInspectable: isInspectable)
7780 }
7881
79- public init ( url: URL , headers: [ String : String ] , isInspectable: Bool , credentials: WKWebViewCredentials ? = nil , preventDeeplink: Bool , blankNavigationTab: Bool , enabledSafeBottomMargin: Bool , enabledSafeTopMargin: Bool = true ) {
82+ public init ( url: URL , headers: [ String : String ] , isInspectable: Bool , credentials: WKWebViewCredentials ? = nil , preventDeeplink: Bool , blankNavigationTab: Bool , enabledSafeBottomMargin: Bool , enabledSafeTopMargin: Bool = true , allowWebViewJsVisibilityControl : Bool = false ) {
8083 super. init ( nibName: nil , bundle: nil )
8184 self . blankNavigationTab = blankNavigationTab
8285 self . enabledSafeBottomMargin = enabledSafeBottomMargin
8386 self . enabledSafeTopMargin = enabledSafeTopMargin
8487 self . source = . remote( url)
8588 self . credentials = credentials
89+ self . allowWebViewJsVisibilityControl = allowWebViewJsVisibilityControl
8690 self . setHeaders ( headers: headers)
8791 self . setPreventDeeplink ( preventDeeplink: preventDeeplink)
8892 self . initWebview ( isInspectable: isInspectable)
8993 }
9094
91- public init ( url: URL , headers: [ String : String ] , isInspectable: Bool , credentials: WKWebViewCredentials ? = nil , preventDeeplink: Bool , blankNavigationTab: Bool , enabledSafeBottomMargin: Bool , enabledSafeTopMargin: Bool = true , blockedHosts: [ String ] ) {
95+ public init ( url: URL , headers: [ String : String ] , isInspectable: Bool , credentials: WKWebViewCredentials ? = nil , preventDeeplink: Bool , blankNavigationTab: Bool , enabledSafeBottomMargin: Bool , enabledSafeTopMargin: Bool = true , blockedHosts: [ String ] , allowWebViewJsVisibilityControl : Bool = false ) {
9296 super. init ( nibName: nil , bundle: nil )
9397 self . blankNavigationTab = blankNavigationTab
9498 self . enabledSafeBottomMargin = enabledSafeBottomMargin
9599 self . enabledSafeTopMargin = enabledSafeTopMargin
96100 self . source = . remote( url)
97101 self . credentials = credentials
102+ self . allowWebViewJsVisibilityControl = allowWebViewJsVisibilityControl
98103 self . setHeaders ( headers: headers)
99104 self . setPreventDeeplink ( preventDeeplink: preventDeeplink)
100105 self . setBlockedHosts ( blockedHosts: blockedHosts)
101106 self . initWebview ( isInspectable: isInspectable)
102107 }
103108
104- public init ( url: URL , headers: [ String : String ] , isInspectable: Bool , credentials: WKWebViewCredentials ? = nil , preventDeeplink: Bool , blankNavigationTab: Bool , enabledSafeBottomMargin: Bool , enabledSafeTopMargin: Bool = true , blockedHosts: [ String ] , authorizedAppLinks: [ String ] ) {
109+ public init ( url: URL , headers: [ String : String ] , isInspectable: Bool , credentials: WKWebViewCredentials ? = nil , preventDeeplink: Bool , blankNavigationTab: Bool , enabledSafeBottomMargin: Bool , enabledSafeTopMargin: Bool = true , blockedHosts: [ String ] , authorizedAppLinks: [ String ] , allowWebViewJsVisibilityControl : Bool = false ) {
105110 super. init ( nibName: nil , bundle: nil )
106111 self . blankNavigationTab = blankNavigationTab
107112 self . enabledSafeBottomMargin = enabledSafeBottomMargin
108113 self . enabledSafeTopMargin = enabledSafeTopMargin
109114 self . source = . remote( url)
110115 self . credentials = credentials
116+ self . allowWebViewJsVisibilityControl = allowWebViewJsVisibilityControl
111117 self . setHeaders ( headers: headers)
112118 self . setPreventDeeplink ( preventDeeplink: preventDeeplink)
113119 self . setBlockedHosts ( blockedHosts: blockedHosts)
@@ -596,7 +602,7 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
596602 }
597603 }
598604
599- func injectJavaScriptInterface ( ) {
605+ private func mobileAppScriptSource ( ) -> String {
600606 let extraControls = allowWebViewJsVisibilityControl ? """
601607 ,
602608 hide: function() {
@@ -606,7 +612,7 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
606612 window.webkit.messageHandlers.show.postMessage(null);
607613 }
608614 """ : " "
609- let script = """
615+ return """
610616 if (!window.mobileApp) {
611617 window.mobileApp = {
612618 postMessage: function(message) {
@@ -620,6 +626,19 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
620626 };
621627 }
622628 """
629+ }
630+
631+ private func addMobileAppUserScript( to userContentController: WKUserContentController ) {
632+ let script = WKUserScript (
633+ source: mobileAppScriptSource ( ) ,
634+ injectionTime: . atDocumentStart,
635+ forMainFrameOnly: true
636+ )
637+ userContentController. addUserScript ( script)
638+ }
639+
640+ func injectJavaScriptInterface( ) {
641+ let script = mobileAppScriptSource ( )
623642 DispatchQueue . main. async {
624643 self . webView? . evaluateJavaScript ( script) { result, error in
625644 if let error = error {
@@ -666,6 +685,7 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
666685 forMainFrameOnly: false
667686 )
668687 userContentController. addUserScript ( script)
688+ addMobileAppUserScript ( to: userContentController)
669689
670690 webConfiguration. allowsInlineMediaPlayback = true
671691 webConfiguration. userContentController = userContentController
0 commit comments