Passing URL with prepare for segue to WKWebview and setting title

I’m attempting to pass an URL with prepare for segue in this viewController from a button press. Everything I’ve tried and read online has been an old version of swift or not a viable solution for Swift 3.1 I’m having trouble also setting the title of the navigation bar to the WKWebview website title. Both issues I’ve been searching for weeks. Hopefully, I can find some help here. Thanks!

 

 

    @IBAction func easyBtnPressed(_ sender: Any) {

        let randomArticle = articles[Int(arc4random_uniform(UInt32(articles.count)))]

        let easyUrl = URL (string: “https:/

        performSegue(withIdentifier: “toGame”, sender: easyUrl)

     

    }

 

 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

            if let destination = segue.destination as? Game{

                if let easyURLStart = sender as? URL{

                    destination.startingURL = easyURLStart

                }

            }

        }

 

 

//Second VC named Game

import UIKit

import WebKit

class Game: UIViewController, WKNavigationDelegate{

    @IBOutlet weak var destinationLabel: UILabel!

    @IBOutlet weak var timerLabel: UILabel!

    @IBOutlet weak var webViewContainer: UIView!

    @IBOutlet weak var backgroundImage: UIImageView!

   

  

    var webView: WKWebView!

    var timer = Timer()

    var timerCount: Double = 0.0

    var startTime = Timer()

    var startingURL = URL(string: “”)

    var endingURL = URL(string: “”)

    var clickedLinks = [String]()

    var clickCount = 0

  

    override func viewDidLoad() {

        super.viewDidLoad()

      

        timer = Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: (#selector(Game.countTimerIncrease)), userInfo: nil, repeats: true)

        RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)

    }

  

    override func viewDidAppear(_ animated: Bool) {

        self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50.0)

        let webView = WKWebView(frame: .zero)

        webViewContainer.addSubview(webView)

      

        webView.translatesAutoresizingMaskIntoConstraints = false

        let height = NSLayoutConstraint(item: webView, attribute: .height, relatedBy: .equal, toItem: webViewContainer, attribute: .height, multiplier: 1, constant: 0)

        let width = NSLayoutConstraint(item: webView, attribute: .width, relatedBy: .equal, toItem: webViewContainer, attribute: .width, multiplier: 1, constant: 0)

        let leftConstraint = NSLayoutConstraint(item: webView, attribute: .leftMargin, relatedBy: .equal, toItem: webViewContainer, attribute: .leftMargin, multiplier: 1, constant: 0)

        let rightConstraint = NSLayoutConstraint(item: webView, attribute: .rightMargin, relatedBy: .equal, toItem: webViewContainer, attribute: .rightMargin, multiplier: 1, constant: 0)

        let bottomContraint = NSLayoutConstraint(item: webView, attribute: .bottomMargin, relatedBy: .equal, toItem: webViewContainer, attribute: .bottomMargin, multiplier: 1, constant: 0)

        webViewContainer.addConstraints([height, width, leftConstraint, rightConstraint, bottomContraint])

      

      

      

        let myRequest = URLRequest(url: startingURL!)

        webView.load(myRequest as URLRequest)

        webView.scrollView.bounces = false

      

    }

Powered by WPeMatico

About

You may also like...

Comments are closed.