Reload default URL on Home button

I have built an app from my web site using Storyboard and view controllers. I have five tabs which all calls different URL's from my web site. Everything works perfect, except one thing: When the Home tab launches and I click on a link on the web site it opens the link in the same view controller. That is fine. But, when I want to click on the Home button again, it does not reload the site - it stays on that same URL still. So instead of going to the default URL it stays on the link address that I clicked on which is mydomain/somelinkurl. How can I make the Home button (which is a Tab Bar Controller) to reload my website again? One note: If I click on any of the other buttons in the Tab bar, and then click on Home again, THEN it reloads the site as it should. Hope you understand my issue and that someone knows how to fix this. Thank you very much in advance! /Lise

Replies

Can't you just put the code that loads the initial URL in the viewWillAppear method? It'll go like this:

  • Launch app.
  • App will initially show the Home tab.
  • viewWillAppear is called in the Home tab, and loads the initial URL.
  • User taps a link on that page, goes to a different URL.
  • User taps the second tab.
  • The second tab's URL is loaded, etc.
  • User tips the Home tab again.
  • viewWillAppear is called, and loads the initial URL again.

Hi! Thank you very much for your answer. That is working actually, the flow that you are describing above. The problem occurs if the user DONT click on a second tab, but instead clicks directly on the Home tab to come back to the start screen again. Then the viewWillAppear does not do anything.

The steps that works are marked with an X:

X Launch app. X App will initially show the Home tab. X viewWillAppear is called in the Home tab, and loads the initial URL. X User taps a link on that page, goes to a different URL. -> This step is working, but not if the user wants to go directly to the start screen again (like a BACK function): User taps the second tab. X The second tab's URL is loaded, etc. X User tips the Home tab again. X viewWillAppear is called, and loads the initial URL again.

So again, its kind of a BACK function I would like to simulate when the user clicks on "Home".

Hope this clears my question :)

Thanks!

What a mess my list became in the answer, here are the steps in a more clear order:

The steps that works are marked with an X:

  • X Launch app.
  • X App will initially show the Home tab.
  • X viewWillAppear is called in the Home tab, and loads the initial URL.
  • X User taps a link on that page, goes to a different URL.
  • -> User taps the second tab. This step is working, but NOT if the user wants to go directly to the start screen again (like a BACK function)
  • X The second tab's URL is loaded, etc.
  • X User tips the Home tab again.
  • X viewWillAppear is called, and loads the initial URL again.

You can edit a post if you need to change it within an hour.

Anyways, so you need the Home tab to go to the initial URL when the tab is tapped, regardless of whether or not the Home tab is currently selected? So, if the user is on the Home tab and they've navigated away by tapping a link on the page and they tap the tab you want it to go back to the initial URL?

This page seems to have a bunch of solutions: https://stackoverflow.com/questions/33837475/detect-when-a-tab-bar-item-is-pressed

There's a Swift 5 version about half way down the page.

None of the examples I found in that article helped unfortunately. I am sure I am missing something! (I started using Xcode last week)

The setup I have is one Tab Bar Controller, 4 Navigation Controllers connected to 4 ViewControllers. And the 4 tabs loads 4 different URL's. On the first tab, I simply want to be able to go back if the user clicks on ANY link in the first screen. I don't have any buttons to click (that would be very easy to fix!) but instead I have a web site that you can scroll on, to click a link just anywhere on the page. So I cannot use a Button and tell it where to go. I don't get how to tell the first ViewController how it should load the links in a new window, or in a new ViewController? And then show a Back button or to be able to click on the Home button to load the website again to the default URL.

The initial ViewController code is here:

import UIKit import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

@IBOutlet weak var TabOne: WKWebView!

var webView : WKWebView!


override func loadView() {
    webView = WKWebView ()
    webView.navigationDelegate = self
    view = webView
    
}


override func viewDidLoad() {
    super.viewDidLoad()
    
    // Do any additional setup after loading the view.
    
    let url = URL(string:"https://www.gigz.se/")
    let request = URLRequest(url: url!)
    
    webView.load(request)
    webView.allowsBackForwardNavigationGestures = true
    self.webView.load(request)
    webView.reload()
    webView.isOpaque = false
    webView.backgroundColor = .black
        }
    
}

Thanks!