Skip to content

Instantly share code, notes, and snippets.

@knight6700
Created January 9, 2024 20:31
Show Gist options
  • Save knight6700/1613c57bc31496ec285a4b184b16e1c9 to your computer and use it in GitHub Desktop.
Save knight6700/1613c57bc31496ec285a4b184b16e1c9 to your computer and use it in GitHub Desktop.
handle a webView
@IBOutlet var containerStack: UIStackView!
func webView(
_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures
) -> WKWebView? {
if navigationAction.targetFrame == nil {
let tempURL = navigationAction.request.url
var components = URLComponents()
components.scheme = tempURL?.scheme
components.host = tempURL?.host
components.path = (tempURL?.path)!
if !(components.url?.absoluteString.isEmpty ?? false) {
let webViewtemp = WKWebView(frame: view.bounds, configuration: configuration)
webViewtemp.uiDelegate = self
if #available(iOS 16.4, *) {
webViewtemp.isInspectable = true
}
webViewtemp.navigationDelegate = self
containerStack.arrangedSubviews[0].isHidden = true
containerStack.addArrangedSubview(webViewtemp)
return webViewtemp
} else {
webView.load(navigationAction.request)
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment