how to add navigation controller to WebView? - swift

I have used the following code to create a webView app in swift. but I need to also add a navigation bar at the bottom that goes to different pages of the website. And those are just another link but the webView is taking the whole screen and I can't seem to add the Nav bar to it. Help is appreciated in advance.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBAction func goBack(_ sender: Any) {
self.webView.goBack()
}
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://test.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}

In Xib or storyboard add a subview to the bottom And change web view bottom constraint constant value.
WebView constraint
SubView constraint

Related

Disable pop-up window - WKWebView SwiftUI

It is possible to show websites with the help of UIKit and a WKWebView in SwiftUI:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "http://bla.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
I know that you can disable cookies by writing:
myRequest.httpShouldHandleCookies = false
But is there a way to avoid showing the pop up window on websites and reject all cookies that are not needed?

Wkwebview overlying and hiding the toolbar

I am using wkwebview to show a webpage but it is overlaying the toolbar. If I remove
view = webView Then the toolbar can be seen but not the webpage. Been going around in circles with this and tried several possible solutions in other threads but nothing is working. Here is my code...
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate{
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string:"website address")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
}
}

Need help to reload WKWebView from tabbed navigation in Swift

I'm trying to make this reload when a user clicks on the tab. I tried a simple reload but didn't work.
#IBOutlet weak var TabOne: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.google.com")
let urlRequest = URLRequest(url:url!)
TabOne.load(urlRequest)
}
Move the reload code into the viewWillAppear(_:)
as viewDidLoad() will only be called once in the view controller life cycle, while viewWillAppear(_:) will be called every time the view is going to appear on the screen.
This is the code that ended up working.
import UIKit
import WebKit
class FirstViewController: UIViewController {
#IBOutlet weak var TabOne: WKWebView!
override func viewWillAppear(_ animated: Bool) {
let url = URL(string: "https://www.google.com")
let urlRequest = URLRequest(url:url!)
TabOne.load(urlRequest)
}
}

Application starting in webview xcode 9

Pretty new to swift & xcode 9. I have my webview code working and displaying the proper link, but it opens the app with it. I have a few different controllers before it should be displayed (ultimately button triggered). When I have this code in, it doesn't change the default view controller as link, it simply loads the webview right away. Any help would be greatly appreciated!
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// 1
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
// 2
let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
toolbarItems = [refresh]
navigationController?.isToolbarHidden = false
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Storyboard / Swift all IBOutlets and _view are nil

I'm working on a Swift app and running into a crazy odd problem. I have a simple storyboard view controller setup, it has 3 UIButtons a UIImageView and a UIView (as a subview of the main view).
I want to programmatically add a WKWebView to the UIView.
Easy enough right? All of the above are in the Storyboard View, declared in the custom UIViewController class and connected in IB. However at run time, everything is nil. This is a snippet of my code:
#IBOutlet var button1 : UIButton!;
#IBOutlet var button2 : UIButton!;
#IBOutlet var button3 : UIButton!;
#IBOutlet weak var containerForWebView: UIView!
var webView: WKWebView!
override func loadView()
{
}
override func viewDidLoad()
{
super.viewDidLoad()
displayWebPage()
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
}
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
}
private func displayWebPage()
{
webView = WKWebView()
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.frame = CGRect(origin: CGPoint.zero, size: containerForWebView.frame.size)
webView.navigationDelegate = self
containerForWebView.addSubview(webView)
let url = URL(string: "https://www.google.com")!
webView.load(URLRequest(url: url))
}
When the code calls the displayWebPage() method I break on the first line. In the debugger you can see all of the UIViewController properties are nil. The IBOutlets are Nil and the _view variable of the UIViewController itself is nil.
I don't understand why this is happening. Everything is pretty simple and easily connected. I never ran into this sort of issue in Objective-C. Any advice is appreciated!
Remove loadView implementation. Do not override that method, always use viewDidLoad instead.
If you override loadView then you are responsible for creating controller's view and assigning it to self.view.