1

I am new to swift and ios.The View Controller launch always the same link and do do not show the grid menu with buttons that is in second scene.What i am doing wrong here? I have in main.storyboard three scene.

In the first scene is navigation.The second scene is a grid menu with buttons that depending on the button click will open a link in webview located in the 3rd scene.The third scene includes a view that should open a specific link depending on the click button in step 2

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

    var webView :  WKWebView!
    var webi:String =  "https://www.google.al"

    override func viewDidLoad() {
        print("‼️OMG:viewDidLoad", webi)
        super.viewDidLoad()

        let url = URL(string: webi)!
        webView.load(URLRequest(url: url))

        let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
        toolbarItems = [refresh]
        navigationController?.isToolbarHidden = false
    }

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

    @IBAction func Menu1(_ sender: Any) {
        print("‼️OMG:viewDidLoad with menu1")
        webi =  "https://www.menu1.com"

    }

    @IBAction func Menu2(_ sender: Any) {
        print("‼️OMG:viewDidLoad with menu1")
        webi =  "https://www.menu2.com"
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        title = webView.title
    }
}

enter image description here

enter image description here

enter image description here

enter image description here

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
Didi
  • 261
  • 1
  • 4
  • 16
  • 2
    Be aware that `viewDidLoad` is called only **once**. – vadian Jan 16 '19 at 22:21
  • Please can you help me.I am new to ios.I want to build a grid of button that every button open different link.The problem that i have it open directly the webview with google link and does not show the grid menu.I have stucked here. – Didi Jan 16 '19 at 22:25
  • Put the two lines to load the web data into a method and call the method in `viewDidLoad` and in the two `IBAction`s – vadian Jan 16 '19 at 22:30
  • Except the `viewDidLoad` call at once part try making your url request with `reloadIgnoringCacheData` to avoid caching for same request. – TheTiger Jan 17 '19 at 11:12

1 Answers1

1

Your problem is that you change the urlStr webi without reloading the webview again

func refresh() { 
  let url = URL(string: webi)!
  webView.load(URLRequest(url: url))
}
@IBAction func Menu1(_ sender: Any) {
    print("‼️OMG:viewDidLoad with menu1")
    webi =  "https://www.menu1.com"
    self.refresh()
}

@IBAction func menu2(_ sender: Any) {
    print("‼️OMG:viewDidLoad with menu1") // other link
    webi =  "https://www.menu1.com"
    self.refresh()
}

///

Navigation->buttons Menu VC -> webViewVC

while your are in buttons menu

 let vc = ViewController()
 vc.webi = "" // set link according to the clicked button
 self.navigationController?.pushViewController(vc,animated:true)

Look to this Demo

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • During the app launching it shows not the grid menu but it open direct the webview?What i need is after launching the app will need to show the grid menu with buttons and on button clickt it must open a link in webview. – Didi Jan 16 '19 at 22:40
  • by scene do you mean a vc ? also do you make the storyboard entry point or the arrow that appear middle left to the navigation ? – Shehata Gamal Jan 16 '19 at 22:45
  • you must create a vc for the buttons menu , don't implement it inside the webviewVC – Shehata Gamal Jan 16 '19 at 22:51
  • select the navigation in storyboard then do https://stackoverflow.com/questions/27683590/how-to-add-an-entry-point-arrow-to-a-tab-bar-controller – Shehata Gamal Jan 16 '19 at 22:54
  • I have main.Stroryboard and inside it i have 3 Scene ( VC ) 1 Webview 2 grid menu and 3 navigation – Didi Jan 16 '19 at 22:54
  • I will try and let you know it tomorrow.Thanks a lot. – Didi Jan 16 '19 at 22:57
  • Should it be this let vc = // instantiate webviewVc from stroybaord .. inside a button function and second question is : let vc = what i need here to write? Thanks – Didi Jan 17 '19 at 08:52
  • yes but it inside the every menu button action , second since your vc is nearly created programmatically you can use what in edit – Shehata Gamal Jan 17 '19 at 09:26
  • I have upload some picture to show you that the main menu is not showing.It open very fast the webview.What i am missing here?Thanks. – Didi Jan 17 '19 at 10:58
  • All the code is in ViewController.swift file.And the main.Storyboad has 3 Scene inside it. – Didi Jan 17 '19 at 11:01
  • I have uploaded some new Pic @Sh_Khan – Didi Jan 17 '19 at 11:08
  • @Didi why you re-assign the same google link inside the button action ????\ – Shehata Gamal Jan 17 '19 at 11:18
  • Sh_Khan the problem is that the main menu is not opening or is opened very fast. What you have said is true i must change it later.My problem is that wenn the app is launching it open direct the webview..It must open the meun and if i click one button than it must open the specific link according the button. – Didi Jan 17 '19 at 11:23
  • Do you understand my question.The user after the app is launching should see grid menu than if the person click the button it will open a website in the webview. – Didi Jan 17 '19 at 11:30
  • you put both in the same vc , please create a separate class for the grids – Shehata Gamal Jan 17 '19 at 11:31
  • @Didi see the attached demo – Shehata Gamal Jan 17 '19 at 12:45
  • I got this error2019-01-17 12:59:49.635074+0000 IOS-Swift-WebView[27811:329091] -[IOS_Swift_WebView.ViewControllerMenu Menu1:]: unrecognized selector sent to instance 0x7fccc3503b60 2019-01-17 12:59:49.643699+0000 IOS-Swift-WebView[27811:329091] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[IOS_Swift_WebView.ViewControllerMenu Menu1:]: unrecognized selector sent to instance 0x7fccc3503b60' *** First throw call stack: ( 0 CoreFoundation 0x000000010d5af1bb __exceptionPreprocess + 331 – Didi Jan 17 '19 at 13:02
  • it appears that you removed the action method for that button , please follow the demo exactly – Shehata Gamal Jan 17 '19 at 13:11
  • Thank you it works.Please can you tell me how to send some special string with http webview to the webserver. Becuase i want to catch this string in the http server.I have done this thing with android but here i do not know it.webView.getSettings().setUserAgentString("didi"); – Didi Jan 17 '19 at 13:35