[英]iOS - How to create WKWebView Back Button programmatically?
我正在嘗試為我的iOS WKWebView應用程序創建一個后退按鈕,這樣當用戶點擊按鈕時它會將它們帶到之前的WKWebView頁面。 我已經找到了很多關於如何使用IB而不是直接代碼的教程。 這是我到目前為止:
原始ViewController.swift:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
private var webView: WKWebView!
let wv = WKWebView(frame: UIScreen.mainScreen().bounds) //needed for working webview
self.webView = WKWebView(frame: frame)
self.webView.navigationDelegate = self
func rightbutton(text: String, action: Selector) {
let rightButton = UIBarButtonItem(title: text, style: .Plain, target: self, action: action)
self.navigationItem.rightBarButtonItem = rightButton
}
func action() {
if self.webView.canGoBack {
print ("Can go back")
self.webView.goBack()
self.webView.reload()
} else {
print ( "Can't go back")
}
}
override func viewDidLoad() {
super.viewDidLoad()
guard let url = NSURL(string: "http://communionchapelefca.org/app-home") else { return }
wv.navigationDelegate = self
wv.loadRequest(NSURLRequest(URL: url))
view.addSubview(wv)
webView.goBack()
webView.reload()
prefButton()
//backButton()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "receiveNotification:",
name: "BeaconServiceRegionEnter",
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "receiveNotification:",
name: "BeaconServiceRegionUpdate",
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "receiveNotification:",
name: "BeaconServiceRegionExit",
object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func prefButton () {
let image = UIImage(named: "icon-pref128") as UIImage?
let button = UIButton(type: UIButtonType.Custom) as UIButton
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
button.frame = CGRectMake(screenWidth * 0.85, screenHeight * 0.90, 56, 56) // (X, Y, Height, Width)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "buttonClicked:", forControlEvents:.TouchUpInside)
self.view.addSubview(button)
}
func buttonClicked(sender:UIButton)
{
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}
func receiveNotification(notification: NSNotification) {
print(notification.userInfo)
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .LinkActivated {
if let newURL = navigationAction.request.URL,
host = newURL.host where !host.containsString("communionchapelefca.org") &&
UIApplication.sharedApplication().canOpenURL(newURL) {
if UIApplication.sharedApplication().openURL(newURL) {
print(newURL)
print("Redirected to browser. No need to open it locally")
decisionHandler(.Cancel)
} else {
print("browser can not url. allow it to open locally")
decisionHandler(.Allow)
}
} else {
print("Open it locally")
decisionHandler(.Allow)
}
} else {
print("not a user click")
decisionHandler(.Allow)
}
}
}
有什么建議? 提前致謝。
編輯:附加整個ViewController而不是代碼片段。
編輯2:k8mil的答案很接近,因為他的答案沒有加載初始網址。 以下是他回答的唯一調整:
private func createWebView() {
guard let url = NSURL(string: "http://communionchapelefca.org/app-home") else { return }
let frame = UIScreen.mainScreen().bounds // or different frame created using CGRectMake(x,y,width,height)
self.webView = WKWebView(frame: frame)
self.webView.navigationDelegate = self
self.webView.loadRequest(NSURLRequest(URL: url))
self.view.addSubview(self.webView)
}
嘗試在WKWebView實例上webView.goBack()
webView.goBack webView.goBack()
方法,而不是WKWebView.goBack()
。 此外后goBack()
你可以調用webView.reload()
方法,以確保你是正確的頁面上。
我編輯了一下你的代碼
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
private var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = NSURL(string: "http://communionchapelefca.org/app-home") else {
return
}
//create WebView and Back button
createWebView()
backButton()
prefButton()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "receiveNotification:",
name: "BeaconServiceRegionEnter",
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "receiveNotification:",
name: "BeaconServiceRegionUpdate",
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "receiveNotification:",
name: "BeaconServiceRegionExit",
object: nil)
}
private func createWebView() {
let frame = UIScreen.mainScreen().bounds // or different frame created using CGRectMake(x,y,width,height)
self.webView = WKWebView(frame: frame)
self.webView.navigationDelegate = self
self.view.addSubview(self.webView)
}
func addBackButton(text: String, action: Selector) {
//this function will add Button on your navigation bar e
let rightButton = UIBarButtonItem(title: text, style: .Plain, target: self, action: action)
self.navigationItem.rightBarButtonItem = rightButton
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func prefButton() {
let image = UIImage(named: "icon-pref128") as UIImage?
let button = UIButton(type: UIButtonType.Custom) as UIButton
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
button.frame = CGRectMake(screenWidth * 0.85, screenHeight * 0.90, 56, 56) // (X, Y, Height, Width)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "buttonClicked:", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
func backButton() {
let image = UIImage(named: "icon-back") as UIImage?
let button = UIButton(type: UIButtonType.Custom) as UIButton
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
button.frame = CGRectMake(screenWidth * 0.85, screenHeight * 0.90, 56, 56) // (X, Y, Height, Width)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "customGoBack:", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
func customGoBack(sender: UIButton) {
if self.webView.canGoBack {
print("Can go back")
self.webView.goBack()
self.webView.reload()
} else {
print("Can't go back")
}
}
func buttonClicked(sender: UIButton) {
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}
func receiveNotification(notification: NSNotification) {
print(notification.userInfo)
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .LinkActivated {
if let newURL = navigationAction.request.URL,
host = newURL.host where !host.containsString("communionchapelefca.org") &&
UIApplication.sharedApplication().canOpenURL(newURL) {
if UIApplication.sharedApplication().openURL(newURL) {
print(newURL)
print("Redirected to browser. No need to open it locally")
decisionHandler(.Cancel)
} else {
print("browser can not url. allow it to open locally")
decisionHandler(.Allow)
}
} else {
print("Open it locally")
decisionHandler(.Allow)
}
} else {
print("not a user click")
decisionHandler(.Allow)
}
}
}
此外,您不需要另一個WKWebView實例,因為您之前已聲明過:
private var webView : WKWebView!
所以沒有必要:
wv.navigationDelegate = self
wv.loadRequest(NSURLRequest(URL: url))
view.addSubview(wv)
對我來說它有效。
希望這個能對您有所幫助 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.