'WKWebview landscape

I want rotate WkWebview but set Orientation did not working.

When loading the pages or pdf files, the orientation turns,
but in other cases(ppt, png, website...), it does not turn as like it were fixed.

It's so weird. It's the same code and only the files are different!

this is my code

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var orientations: UIInterfaceOrientationMask = [.portrait]
    
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return self.orientations
    }
}
class viewController: UIViewController {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let orientation: UIInterfaceOrientationMask = .portrait
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return self.orientation
    }
    override var shouldAutorotate: Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        guard let url = Bundle.main.url(forResource: "docExample", withExtension: "doc") else { return }

        let webView = WKWebView(frame: self.view.frame)
        webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        webView.loadFileURL(url, allowingReadAccessTo: url)
        webView.navigationDelegate = self
        self.view.addSubview(webView)
  
        let button = UIButton()
        button.frame.size = CGSize(width: 100, height: 100)
        button.center = self.view.center
        button.backgroundColor = .brown
        button.addTarget(self, action: #selector(self.tapOrientationButton(:_)), for: .touchUpInside)
        self.view.addSubview(button)
    }

    @objc
    private func tapOrientationButton(_ button: UIButton) {
        if self.orientation == .portrait {
            self.setOrientation(.landscapeLeft)
        } else
        if self.orientation == .landscapeLeft {
            self.setOrientation(.portrait)
        }
    }

    func setOrientation(_ orientation: UIInterfaceOrientation) {
        if orientation == .portrait {
            self.appDelegate.orientations = [.portrait]
            self.orientation = .portrait
            UIDevice.current.setValue(orientation.rawValue, forKey: "orientation")
        } else
        if orientation == .landscapeLeft {
            self.appDelegate.orientations = [.portrait, .landscapeLeft]
            self.orientation = .landscapeLeft
            UIDevice.current.setValue(orientation.rawValue, forKey: "orientation")
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source