Need help to reload WKWebView from tabbed navigation in Swift - 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)
}
}

Related

Swift webView empty spaces on scroll

When I'm scrolling up or down and no space for scroll, there are empty spaces. How can I fix it?
Empty places I marked with a red lines.
code
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let aurl = URL(string: "https://www.google.com")
let arequest = URLRequest(url: aurl!)
webView.loadRequest(arequest)
}
}
I fix it.
webView.scrollView.bounces = false

Grab loaded webpage URL? WKWebView macOS

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.urlText.stringValue = String(describing: webView.url!)
}
This does not work 100% of the time.
An example would be when I navigate to reddit. I click a thread and it changes URL which is fine, but if I click the reddit home button, it doesn't change URL to www.reddit.com even after it is 100% loaded.
Any help is appreciated.
What else is going on in your view controller? I'm not able to reproduce your problem with the code below. Have you tried this in an isolated project?
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var webView: WKWebView!
#IBOutlet weak var urlLabel: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
let req = URLRequest(url:
URL(string: "https://reddit.com")!)
webView.load(req)
}
#IBAction func getUrlClicked(_ sender: Any) {
guard let url = webView.url else { return }
urlLabel.stringValue = url.absoluteString
print("url: \(String(describing: webView.url!))")
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}

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.

SWIFT Can't Open Second URL in UIWebView with Second Button

Boy am I a noob. Can't get a button to open a second URL in SWIFT. App opens, loads URL in WebView upon open. Now I want a button to open another url in the WebView when clicked. (Back button, go forward button and reload no prob)... Please help, thank you
import UIKit
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
var URLPath = "http://bandersnatchorlando.com/x19559"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadAddressURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadAddressURL(){
let requestURL = NSURL(string:URLPath)
let request = NSURLRequest(URL:
requestURL)
webView.loadRequest(request)
}
#IBOutlet weak var reLoad: UIButton!
}
Assuming you already have the other button in your Storyboard and it is linked to an #IBAction then inside the action:
let url = NSURL(string:"http://myotherurl.com")
let request = NSURLRequest(URL:url)
webView.loadRequest(request)

Swift Web Browser

I am extremely new to swift, and have no experience with objective-C, only partial C++.
I am trying to create a simple web browser with swift, this is the following code I have:
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var containerView: UIView!
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
var url = NSURL(string: "https://www.duckduckgo.com/")
var req = NSURLRequest(URL: url!)
self.webView!.loadRequest(req)
}
#IBAction func Searchresult(sender: AnyObject) {
url = NSURL(string: "https://www.duckduckgo.com/?q=\(sender)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Supposedly XCode says Use of unresolved identifier URL with #IBAction func Searchresult.
Once again I am greatly sorry if there is an obvious fix, I am extremely tired and not good at swift.
at first sight seems that you didn't declare url inside #IBAction, just put let url = NSURL(string: "https://www.duckduckgo.com/?q=\(sender)")