如何在Swift中向响应式网站添加后退按钮

时间:2018-11-02 13:30:54

标签: ios swift webview responsive-design uibutton

我使用WKWebView创建了一个小型应用程序,该应用程序允许用户快速访问响应型网站,登录页面具有一个选项,允许用户一次访问整个公司网站在显示网站页面时,我没有“后退按钮”选项可将您带回到上一页。如何快速添加后退按钮,用户可以选择将其返回到原始的响应式网站页面。我是新来的,任何帮助将不胜感激。我包括我的代码的副本(ViewController.swift)。

       ViewController.swift
       //  wfmApp
       //
       //  Created by Stefan Sanders on 9/13/18.
       //  Copyright © 2018 Stefan Sanders. All rights reserved.
       //

    import UIKit
    import WebKit

    class ViewController: UIViewController {

    @IBOutlet weak var wenView: WKWebView!

    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let URLString = "https://www.wastefreemail.com/app"
        let request = URLRequest(url:URL(string:URLString)!)
        self.wenView.load(request)

        self.wenView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "loading" {
            if wenView.isLoading {
                activityIndicator.startAnimating()
                activityIndicator.isHidden = false
            } else {
                activityIndicator.stopAnimating()
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

3 个答案:

答案 0 :(得分:1)

此代码返回上一页

import UIKit
import WebKit

class ViewController: UIViewController {

    @IBOutlet weak var wenView: WKWebView!

    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let URLString = "https://www.wastefreemail.com/app"
        let request = URLRequest(url:URL(string:URLString)!)
        self.wenView.load(request)

        self.wenView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "loading" {
            if wenView.isLoading {
                activityIndicator.startAnimating()
                activityIndicator.isHidden = false
            } else {
                activityIndicator.stopAnimating()
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func goBack(_ sender: Any) {
        wenView.goBack()
    }

}

答案 1 :(得分:0)

请检查Wkwebview backForwardList,您将获得完整的前进和后退项目列表,从列表中可以获得backItem,并且可以检查backitem是否为nil,这意味着您可以返回并与您可以获得forwarditem并可以显示前进按钮。有关更多信息,请参见此链接back forward list

答案 2 :(得分:0)

已快速更新5。

$scope.pendingRequests++;

let url = '/whatever_Your_URL_Is'
$http.get(url)
  .then(function(response) {
      $scope.pendingRequests--;
})