skip to Main Content

My webView Back & Forward buttons used to work but they are not anymore. I’ve looked everywhere I could for a solution but only found similar ones including here but nothing works for me or for Swift (at least to me). Below are my codes.I have Webkit Framework added, still not working when the buttons did work before even without adding the Framework. App Transport Security setting is set to YES for Arbitrary Load as before when the buttons were working. Did Apple change something? Please help. Thanks!!!

import UIKit
import WebKit

class SocialPlatformController: UIViewController, WKNavigationDelegate {
    
    @IBOutlet weak var BackBtn: UIButton!
    
    @IBOutlet weak var ForwardBtn: UIButton!
    
    @IBOutlet weak var webView: WKWebView!
    
    @IBAction func backToMainScreen(_ sender: Any) {
    }
    
    @IBAction func goBackBtnTap(_ sender: Any) {
        if webView.canGoBack {
            webView.goBack()
        }
    }
    
    @IBAction func goForwardBtnTap(_ sender: Any) {
        if webView.canGoForward {
            webView.goForward()
        }
    }

    
    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Swift.Void) {
        
        BackBtn.isEnabled = webView.canGoBack
        ForwardBtn.isEnabled = webView.canGoForward
        guard
            let response = navigationResponse.response as? HTTPURLResponse,
            let url = navigationResponse.response.url
        else {
            decisionHandler(.cancel)
            return
        }

        if let headerFields = response.allHeaderFields as? [String: String] {
            let cookies = HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: url)
            cookies.forEach { (cookie) in
                HTTPCookieStorage.shared.setCookie(cookie)
            }
        }

        decisionHandler(.allow)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        webView.navigationDelegate = self
        
        let url:URL = URL(string:"https://www.facebook.com/groups/246328283932460")!
        let urlRequest:URLRequest = URLRequest(url: url)
        webView.load(urlRequest)


    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override public func viewDidAppear(_ animated: Bool) {

        super.viewDidAppear(animated)

        }
    
}

2

Answers


  1. Perhaps it’s better to handle the Back and Forward button state in a navigation delegate handler that’s fired after the navigation has been finished, for example:

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
    
    Login or Signup to reply.
  2. //successful load
        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {     
            updateButtons()
        }
       //failure load
        func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
                updateButtons()
            }
        //back-forward button update
        func updateButtons() {
                backButton.isEnabled = webView.canGoBack
                forwardbutton.isEnabled = webView.canGoForward
            }
    

    You can follow these delegates and methods. I think buttons are not getting updated/loaded as they should.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search