I have a view controller (WKWebViewController) that is embedded in a NavigationController. This ViewController presents a WKWebView. After navigating to any web page; and upon long-pressing any detected content, such as a phone number or a link, an action sheet is displayed with options like copy, share, etc. The issue is when this action sheet is dismissed, the WKWebViewController gets dismissed along with it and the root ViewController is displayed! Regardless of the what the selection was be it Copy, Cancel, or even if tapped anywhere on the screen.
I've tried overriding the "present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)" and the "dismiss(animated flag: Bool, completion: (() -> Void)?)" in an attempt to understand what is happening but then realized that the action sheet is not being presented neither dismissed by its parent view controller (WKWebViewController), the matter of fact I did the same on the root view controller and found that it is not presented on it neither.
I've done a lot of searching trying to understand what is causing this behavior, I even built a new project with a simple WKWebView only and always ended up with the same problem.
Here is the code:
import UIKit; import WebKit
class WKWebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var destinationUrlString: String?
var myWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
webConfiguration.dataDetectorTypes = []
let origin = CGPoint(x: 0, y: 0)
let size = CGSize(width: view.frame.size.width, height: view.frame.size.height)
myWebView = WKWebView(frame: .init(origin: origin, size: size), configuration: webConfiguration)
myWebView.uiDelegate = self
myWebView.navigationDelegate = self
myWebView.allowsLinkPreview = false
view = myWebView
destinationUrlString = "https://www.stackoverflow.com"
guard let url = URL(string: destinationUrlString!) else {return}
print(url)
let request = URLRequest(url: url)
myWebView.load(request)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
//show progress indicator
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//dismiss progress indicator
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
//show error
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
//show error
}
}
I've also attached a GIF showing the issue:
I am using Xcode 9.3 (9E145) and Swift 4.1.
Am I missing something? How can this be fixed? Any help would be really appreciated.
.cancel
style action? – thorb65