Skip to content

Commit

Permalink
- ClientWebAppViewController
Browse files Browse the repository at this point in the history
	- add .shouldSendCloseEvent property to close via window.close(), allowing signalling inside the web view that it is being closed
	- disallow dismissal via gestures
  • Loading branch information
felix-schwarz committed Sep 19, 2022
1 parent 6055bd9 commit 4b9af63
Showing 1 changed file with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@

import UIKit
import WebKit
import ownCloudAppShared

class ClientWebAppViewController: UIViewController {
class ClientWebAppViewController: UIViewController, WKUIDelegate {
var urlRequest: URLRequest
var webView: WKWebView?

var shouldSendCloseEvent: Bool = true

init(with urlRequest: URLRequest) {
self.urlRequest = urlRequest

super.init(nibName: nil, bundle: nil)

self.isModalInPresentation = true
}

required init?(coder: NSCoder) {
Expand All @@ -48,6 +53,7 @@ class ClientWebAppViewController: UIViewController {

webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
webView?.translatesAutoresizingMaskIntoConstraints = false
webView?.uiDelegate = self

rootView.addSubview(webView!)

Expand All @@ -65,7 +71,18 @@ class ClientWebAppViewController: UIViewController {
super.viewDidLoad()

navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .close, primaryAction: UIAction(handler: { [weak self] _ in
self?.dismiss(animated: true)
if self?.shouldSendCloseEvent == true {
// Close via window.close(), which is calling dismissSecurely() once done
self?.closeWebWindow()

// Call dismissOnce() after 10 seconds regardless
OnMainThread(after: 10) {
self?.dismissOnce()
}
} else {
// Close directly
self?.closeWebWindow()
}
}))
}

Expand All @@ -77,7 +94,27 @@ class ClientWebAppViewController: UIViewController {
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

// Drop web view
webView?.uiDelegate = nil
webView?.removeFromSuperview()
webView = nil
}

private var isDismissed = false
func dismissOnce() {
if !isDismissed {
isDismissed = true
self.dismiss(animated: true)
}
}

// window.close() handling
func closeWebWindow() {
webView?.evaluateJavaScript("window.close();")
}

// UI delegate
func webViewDidClose(_ webView: WKWebView) {
dismissOnce()
}
}

0 comments on commit 4b9af63

Please sign in to comment.