I want to know if it's possible to disable full-screen mode when WKWebView open local video file by using the file's local url.
Because I always receive error "Plug-in handled load" when WKWebView is loading(but the video is still playing in full screen mode).
I checked some solutions from StackOverFlow:
https://stackoverflow.com/a/10194888/11055217
But these link still cannot solve my problem.
Here is my WKWebView code setup:
//
// WebViewController.swift
// HelpieMeApp
//
// Created by Guests on 11/9/18.
// Copyright © 2018 Menjie Mao. All rights reserved.
//
import WebKit
class WebViewController: UIViewController {
private var webView: WKWebView!
@IBOutlet var viewTitle: UILabel!
@IBOutlet weak var loadingIndicator: UIActivityIndicatorView!
var urlString: String = ""
var url: URL? = nil
var webViewTitle: String = ""
override func viewDidLoad() {
super.viewDidLoad()
self.viewTitle.text = self.webViewTitle
self.initWebView()
self.loadingIndicator.stopAnimating()
self.loadingIndicator.isHidden = true
// Load file url else use load link url to support both ways
if let fileURL = self.url {
self.webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
self.webView.customUserAgent = UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent")
} else {
self.webView.load(URLRequest(url: URL(string: self.urlString)!))
}
}
@IBAction func cancelAction(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
private func initWebView() {
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
// Setup Web View
let configuration = WKWebViewConfiguration()
configuration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
configuration.preferences = preferences
self.webView = WKWebView(frame: CGRect.zero, configuration: configuration)
self.webView.translatesAutoresizingMaskIntoConstraints = false
self.webView.allowsLinkPreview = true
self.webView.allowsBackForwardNavigationGestures = true
self.webView.navigationDelegate = self
self.view.addSubview(self.webView)
self.view.bringSubviewToFront(self.loadingIndicator)
// Add Constraints
self.webView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 80).isActive = true
self.webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
self.webView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0).isActive = true
self.webView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0).isActive = true
if #available(iOS 11.0, *) {
self.webView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
} else {
self.webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
}
}
}
// MARK: - WKNavigationDelegate
extension WebViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
self.loadingIndicator.startAnimating()
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Swift.Void) {
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.loadingIndicator.stopAnimating()
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
self.loadingIndicator.stopAnimating()
self.loadingIndicator.isHidden = true
AlertUtil.showErrorMessage(vc: self, title: error.localizedDescription)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
self.loadingIndicator.stopAnimating()
self.loadingIndicator.isHidden = true
AlertUtil.showErrorMessage(vc: self, title: error.localizedDescription)
}
}
And the error triggered at
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)