WKWebView handling popup windows

Overview

WKWebViewWithPopUp

WKWebView handling pop-up windows

Property

If there is a pop-up window, use the pop-up window. If there is no pop-up window, use the default web view as the current webView.

var popupWebViews = [WKWebView]()
var currentWebView: WKWebView {
    return popupWebViews.isEmpty ? webView : popupWebViews.last!
}

Settings

1. configure javaScript

if #available(iOS 14, *) {
    let preferences = WKWebpagePreferences()
    preferences.allowsContentJavaScript = true
    webView.configuration.defaultWebpagePreferences = preferences
}
else {
    webView.configuration.preferences.javaScriptEnabled = true
    /// user interaction없이 윈도우 창을 열 수 있는지 여부를 나타냄. iOS에서는 기본값이 false이다.
    webView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = true
}

2. back and forward button

func configureBackwardAndForwardButton() {
    /// webView가 뒤로 이동할 수 있거나 팝업창이 있는 경우 뒤로가기 활성화
    backwardButton.isEnabled = currentWebView.canGoBack || popupWebViews.count > 0
    /// webView가 앞으로 이동할 수 있는 경우 활성화
    forwardButton.isEnabled = currentWebView.canGoForward
}

3. backward button action

@IBAction func tapBackwardButton(_ sender: UIBarButtonItem) {
    if currentWebView.canGoBack {
        currentWebView.goBack()
        return
    }

    if popupWebViews.count > 0 {
        let last = popupWebViews.popLast()
        last?.removeFromSuperview()
        /// webView 로드가 완료되면 버튼을 재설정하기는 하지만
        /// 팝업창을 뒤로가기할 경우(닫을 경우) 로드 완료 delegate가 호출되지 않기 때문에 이곳에서 버튼을 재설정한다.
        configureBackwardAndForwardButton()
    }
}

4. create new pop-up

func createNewWebView(_ config: WKWebViewConfiguration) -> WKWebView {
    let newWebView = WKWebView(frame: webView.frame,
                           configuration: config)
    newWebView.navigationDelegate = self
    newWebView.uiDelegate = self
    newWebView.allowsBackForwardNavigationGestures = true
    view.addSubview(newWebView)
    popupWebViews.append(newWebView)
    return newWebView
}

Delegate

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    /// 팝업창(새 탭) 처리
    print("--->[createWebViewWith configuration]")
    return createNewWebView(configuration)
}
You might also like...
IAMPopup is a simple class for expressing custom popup in various forms.
IAMPopup is a simple class for expressing custom popup in various forms.

IAMPopup Introduction IAMPopup is a simple class for expressing custom popup in various forms. This includes where to display the popup and space to d

Simple way to present custom views as a popup in iOS and tvOS.
Simple way to present custom views as a popup in iOS and tvOS.

PopupKit PopupKit is a simple and flexible iOS framework for presenting any custom view as a popup. It includes a variety of options for controlling h

A framework for presenting bars and view controllers as popup, much like the look and feel of Apple Music App.
A framework for presenting bars and view controllers as popup, much like the look and feel of Apple Music App.

PBPopupController PBPopupController is a framework for presenting bars and view controllers as popup, much like the look and feel of Apple Music App.

Simple Swift class for iOS that shows nice popup windows with animation.
Simple Swift class for iOS that shows nice popup windows with animation.

NMPopUpView Simple class for iOS that shows nice popup windows, written in Swift. The project is build with Swift 4.0, so you need Xcode 9.0 or higher

A Swift Popup Module help you popup your custom view easily
A Swift Popup Module help you popup your custom view easily

JFPopup JFPopup is a Swift Module help you popup your custom view easily. Support 3 way to popup, Drawer, Dialog and BottomSheet. Example To run the e

      MarkdownView is a WKWebView based UI element, and internally use bootstrap, highlight.js, markdown-it.
MarkdownView is a WKWebView based UI element, and internally use bootstrap, highlight.js, markdown-it.

MarkdownView is a WKWebView based UI element, and internally use bootstrap, highlight.js, markdown-it.

Swift library for embedding and controlling YouTube videos in your iOS applications via WKWebView!

YouTubePlayer Embed and control YouTube videos in your iOS applications! Neato, right? Let's see how it works. 0.7.0 Update: WKWebView breaking change

Proof-of-concept `WKWebview.evaluateJavaScript(...)` replacement for WebSocket-based Javascript execution.

Proof-of-concept `WKWebview.evaluateJavaScript(...)` replacement for WebSocket-based Javascript execution.

Conveniently getting the User Agent through WKWebView

WebKit User Agent Requirements iOS 9.0+ macOS 10.11+ Installation See the subsec

Otafuku provides utility classes to use WKWebView in Swift.

Otafuku Otafuku provides utility classes to use WKWebView. Features WebViewUIController to handle WKUIDelegate methods by presenting an alert as usual

WebViewCustomFont - WKWebView extension to addScript for inject a custom font-face
WebViewCustomFont - WKWebView extension to addScript for inject a custom font-face

WebViewCustomFont WKWebView extension to addScript for inject a custom font-face

How to handle HelaPay links inside UIWebView and WKWebView

Handling හෙළPay Links Setup Clone this repo. git clone https://github.com/PayHereDevs/helapay-link-handler.git To properly handle හෙළPay Links in you

A Swift event bus for UIWebView/WKWebView and JS.
A Swift event bus for UIWebView/WKWebView and JS.

An event bus for sending messages between UIWebView/WKWebView and embedded JS. Made with pure Swift. Features Easy, fast and reliable event bus system

Modern JavaScriptBridge for JSContext & WKWebview.
Modern JavaScriptBridge for JSContext & WKWebview.

HWJavaScriptBridge Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements iOS 6.0+ Ins

A paging menu controller built from other view controllers placed inside a scroll view (like Spotify, Windows Phone, Instagram)
A paging menu controller built from other view controllers placed inside a scroll view (like Spotify, Windows Phone, Instagram)

Unfortunately, life gets in the way sometimes and I won't be able to maintain this library any longer and upgrade this library to where it needs to be

Windows Empty Swift Application Template

Windows Empty Swift Application Template This provides a template project to build a Swift application on Windows using Swift/Win32. Build Requirement

 💻This is an open source project of the Windows 11 desktop client implemented using SwiftUI.
💻This is an open source project of the Windows 11 desktop client implemented using SwiftUI.

💻 💻This is an open source project of the Windows 11 desktop client implemented using SwiftUI.

Kodi is an award-winning free and open source home theater/media center software and entertainment hub for digital media. With its beautiful interface and powerful skinning engine, it's available for Android, BSD, Linux, macOS, iOS and Windows.
Owner
Hankyeol Park
iOS Developer : )
Hankyeol Park
A Swift Popup Module help you popup your custom view easily

JFPopup JFPopup is a Swift Module help you popup your custom view easily. Support 3 way to popup, Drawer, Dialog and BottomSheet. Example To run the e

逸风 77 Dec 14, 2022
Show MS Windows style activation dialog on my screen.

Activate Mac The "Activate Windows" watermark ported to macOS with Swift Objective-C. Special thanks: MrGlockenspiel/activate-linux for the idea. Inst

Lakr Aream 195 Dec 23, 2022
SSToastMessage is written purely in SwiftUI. It will add toast, alert, and floating message view over the top of any view. It is intended to be simple, lightweight, and easy to use. It will be a popup with a single line of code.

SSToastMessage SSToastMessage is written in SwiftUI. It will add toast, alert, and floating message view over the top of any view. It is intended to b

Simform Solutions 223 Dec 2, 2022
STPopup provides STPopupController, which works just like UINavigationController in popup style, for both iPhone and iPad. It's written in Objective-C and compatible with Swift.

STPopup STPopup provides STPopupController, which works just like UINavigationController in popup style, for both iPhone and iPad. It's written in Obj

Kevin Lin 2.6k Jan 6, 2023
PopupController is a controller for showing temporary popup view.

PopupController PopupController is a controller for showing temporary popup view. Demo Try PopupController on Appetize.io Installation CocoaPods pod '

daisuke sato 338 Dec 14, 2022
A simple, customizable popup dialog for iOS written in Swift. Replaces UIAlertController alert style.

Introduction Popup Dialog is a simple, customizable popup dialog written in Swift. Features Easy to use API with hardly any boilerplate code Convenien

Orderella Ltd. 3.8k Dec 20, 2022
PopupWindow is a simple Popup using another UIWindow in Swift

PopupWindow PopupWindow is a simple Popup using another UIWindow Feature PopupWindow can be displayed at the top or bottom of the screen. Popup can se

shinji hayashi 415 Dec 5, 2022
A lightweight library for popup view

SHPopup SHPop is lightweight library used for popup view Sample One Sample Two Sample Three Features SHPopup supports a popup inside another popup wit

Shezad Ahamed 37 Oct 2, 2022
⛩ Presenting custom views as a popup in iOS.

FFPopup is a lightweight library for presenting custom views as a popup. Bounce from Top & Bounce to Bottom Bounce from Top & Bounce to Top Bounce in

JonyFang 828 Jan 5, 2023
The library allows to create simple popup menus

react-native-popup-menu This library allows to create simple popup menus Installation "react-native-popup-menu": "sergeymild/react-native-popup-menu"

SergeyMild 0 Aug 20, 2022