如何在 Swift 中向响应式网站添加后退按钮
Posted
技术标签:
【中文标题】如何在 Swift 中向响应式网站添加后退按钮【英文标题】:How to add a back button to a Responsive Web-Site in Swift 【发布时间】:2018-11-02 13:30:54 【问题描述】:我使用 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.
【问题讨论】:
ios - How to create WKWebView Back Button programmatically?的可能重复 【参考方案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()
【讨论】:
您添加的 IBAction 函数,它是否连接到 viewController(主情节提要,或者我需要向主情节提要添加一个后退按钮并将其连接到该后退按钮? 您需要在 Main Storyboard 中添加一个按钮并将其与 @IBAction func 链接 感谢 gms,但是如果我在主情节提要中添加一个按钮,那么用户会根据我的代码转到 URL 位置,一旦他们在响应式网站上选择了完整版本的链接,它们位于应用程序之外的网站上,因此我认为该按钮不可用,因为情节提要不再是用户将看到的部分。 gms,稍作调整,您的推荐很棒,谢谢。【参考方案2】:请检查 Wkwebview backForwardList
你会得到完整的后退项目列表,从列表中你可以得到 backItem
并且你可以检查 backitem 是否不为零,这意味着你可以返回并且和你一样可以得到 @ 987654324@ 也可以显示前进按钮。有关更多信息,请参阅此链接back forward list
【讨论】:
【参考方案3】:使用 swift 5 更新。
import UIKit
import WebKit
class WebViewViewController: UIViewController, WKNavigationDelegate
var webView: WKWebView!
override func loadView()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
override func viewDidLoad()
super.viewDidLoad()
let url = URL(string: "https://www.apple.com/in/")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
navigationItem.rightBarButtonItems =
[ UIBarButtonItem(title: ">", style: .plain, target: self, action: #selector(goForward)),
UIBarButtonItem(title: "<", style: .plain, target: self, action: #selector(goBack))
]
let loader = UIActivityIndicatorView()
if webView.isLoading
loader.startAnimating()
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: loader)
else
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(refreshButton))
@objc func goBack()
webView.goBack()
@objc func refreshButton()
navigationController?.popToRootViewController(animated: true)
@objc func goForward()
webView.goForward()
func webView(_ webView: WKWebView, didFinish Navigation: WKNavigation!)
title = "Demo"
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(refreshButton))
【讨论】:
以上是关于如何在 Swift 中向响应式网站添加后退按钮的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 中向 UIViewPropertyAnimator 添加完成处理程序