0

I have a TableView that contains a list of web urls. I'd like to open a browser that navigates to the url that is selected, but with some custom functionality. When I use the SFSafariViewController, I get the full-screen browser. However, when I use the WKWebViewController, I get the modal that you swipe down to dismiss.

How to I get the WKWebView to appear full-screen, instead of like the modal?

Using SFSafariViewController:

class ShoppingViewController: UITableViewController {

    ...

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        showRetailerHome(indexPath: indexPath)
    }

    func showHome(indexPath: IndexPath){

        ...

        if let url = URL(string: urlString){
            let config = SFSafariViewController.Configuration()
            let vc = SFSafariViewController(url: url, configuration: config)
            present(vc, animated: true)
        }        
    }

The result:

enter image description here

Using WKWebViewController:

class ShoppingViewController: UITableViewController {

    ...

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        showRetailerHome(indexPath: indexPath)
    }

    func showHome(indexPath: IndexPath){

        let customWebController = WKBrowserController()
        present(customWebController, animated: true)

    }

The WKBrowserController:

class WKBrowserController: UIViewController, WKNavigationDelegate {

    var webView: WKWebView!

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        self.view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let url = URL(string: "https://www.amazon.com")!
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true
    }
}

The result:

enter image description here

Asperi
  • 228,894
  • 20
  • 464
  • 690
gsamerica
  • 153
  • 1
  • 3
  • 18

2 Answers2

1

When you present a viewcontroller with full screen. You can set it .fullscreen

Try :

let customWebController = WKBrowserController()
customWebController.modalPresentationStyle = .fullScreen
present(customWebController, animated: true)
King.lbt
  • 843
  • 5
  • 15
  • where do you put this code? Sorry for being a real novice here... – Issahar Weiss Jan 15 '21 at 11:33
  • You should put in the the controller you want to present the Web Browser. For example, you click a button and you want to present web. You put this code in that action – King.lbt Jan 19 '21 at 02:42
0
class WKBrowserController: UIViewController, WKNavigationDelegate {

    var Activity: UIActivityIndicatorView!

    var webView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()
        Activity = UIActivityIndicatorView()
        Activity.center = view.center
        view.addSubview(Activity)

        guard let url = URL(string: "your-url-string") else { return }
        webView = WKWebView(frame: self.view.frame)
        webView.translatesAutoresizingMaskIntoConstraints = false
        webView.isUserInteractionEnabled = true
        webView.navigationDelegate = self
        view.addSubview(webView)
        let request = URLRequest(url: url)
        webView.load(request)

        webView.addSubview(Activity)
        Activity.startAnimating()
        webView.navigationDelegate = self
        Activity.hidesWhenStopped = true
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .linkActivated  {
            if let url = navigationAction.request.url,
                let host = url.host, !host.hasPrefix("your-url-string"),
                UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url)
                print(url)
                print("Redirected to browser. No need to open it locally")
                decisionHandler(.cancel)
            } else {
                //print("Open it locally")
                decisionHandler(.allow)
            }
        } else {
            //print("not a user click")
            decisionHandler(.allow)
        }
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        Activity.stopAnimating()
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        Activity.stopAnimating()
    }
}
Adem Özsayın
  • 245
  • 2
  • 14