@@ -120,7 +120,9 @@ struct WebView: UIViewRepresentable {
120120 config. preferences. setValue ( true , forKey: " allowFileAccessFromFileURLs " )
121121
122122 let webView = WKWebView ( frame: . zero, configuration: config)
123-
123+ webView. navigationDelegate = context. coordinator
124+ webView. uiDelegate = context. coordinator
125+
124126 setHandlers. setMessageHandlers ( handlers: MessageHandlers ( webView: webView) )
125127 let source = """
126128 window.runningOnIOS = true;
@@ -138,4 +140,36 @@ struct WebView: UIViewRepresentable {
138140 func updateUIView( _ uiView: UIViewType , context: Context ) {
139141 uiView. load ( URLRequest ( url: URL ( string: scheme + " :/index.html " ) !) )
140142 }
143+
144+ func makeCoordinator( ) -> Coordinator {
145+ Coordinator ( )
146+ }
147+
148+ class Coordinator : NSObject , WKNavigationDelegate , WKUIDelegate {
149+ // Intercept all URLs
150+ func webView( _ webView: WKWebView , decidePolicyFor navigationAction: WKNavigationAction , decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Void ) {
151+ guard let url = navigationAction. request. url else {
152+ decisionHandler ( . cancel)
153+ return
154+ }
155+ print ( " Loading URL: " + url. absoluteString)
156+ // TODO: figure out if there are links (not target=_blank which are handled below) that
157+ // should be intercepted here to be opened in the system browser instead.
158+ // For now, allow everything.
159+ decisionHandler ( . allow)
160+ }
161+
162+ func webView( _ webView: WKWebView , createWebViewWith configuration: WKWebViewConfiguration , for navigationAction: WKNavigationAction , windowFeatures: WKWindowFeatures ) -> WKWebView ? {
163+ // Intercept target=_blank link clicks and open them in the system browser.
164+ // This opens e.g. the cookie policy and other external links in the Moonpay/Pocket widgets, etc.
165+ if navigationAction. targetFrame == nil || !navigationAction. targetFrame!. isMainFrame {
166+ if let url = navigationAction. request. url {
167+ if UIApplication . shared. canOpenURL ( url) {
168+ UIApplication . shared. open ( url)
169+ }
170+ }
171+ }
172+ return nil
173+ }
174+ }
141175}
0 commit comments