1

I am trying to build a very basic browser for in-house use at my company. This is the first real code I've written in Swift, so very much a beginner and relying on tutorials. I have successfully got text to display on a second controller, and would now like to use that to load a Web View.

the setup

class ViewController: UIViewController {

@IBOutlet weak var urlField: UITextField!
@IBAction func launch(_ sender: Any) {
    if urlField.text != "" {
        performSegue(withIdentifier: "launch", sender: self)
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let secondController = segue.destination as! BrowserController
    secondController.urlString = urlField.text!
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

}

This is where I get lost - how can I turn that urlString into something the web view can then load?

All I have so far in the second controller is

import UIKit
import WebKit

class BrowserController: UIViewController {

    @IBOutlet weak var label: UILabel!

    var urlString = String()

    override func viewDidLoad() {
        super.viewDidLoad()

        label.text = urlString

        // Do any additional setup after loading the view.
    }
}

Any help greatly appreciated!

revlis
  • 175
  • 2
  • 11
  • If I understand well, you want to change `urlString` to something the webview can read, then you need to initialize an `URL` object from that string. See that post : https://stackoverflow.com/a/32183827/5464805 – Olympiloutre Jun 13 '19 at 03:36

4 Answers4

1

Replace your second view controller with something like below

 import UIKit
    import WebKit

    class BrowserController: UIViewController, WKNavigationDelegate {

        @IBOutlet weak var label: UILabel!
        var urlString = String()
        var webView : WKWebView!

        override func viewDidLoad() {
            super.viewDidLoad()

            label.text = urlString

            // Do any additional setup after loading the view.
            //Load the webview
            loadPage();
        }

        func loadPage(){
           let url = NSURL(string: myBlog)
           let request = NSURLRequest(URL: url!)

           // init and load request in webview.
           webView = WKWebView(frame: self.view.frame)
           webView.navigationDelegate = self
           webView.loadRequest(request)
           self.view.addSubview(webView)
        }

        //MARK:- WKNavigationDelegate
        func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
           print(error.localizedDescription)
        }

     func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
           print("Strat to load")
       }
     func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
           print("finish to load")
       }
}
Haseeb Afsar
  • 619
  • 5
  • 7
1
import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var webView: UIWebView!
    var urlString = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        webView.delegate = self
        if let url = URL(string: urlString!) {
            let request = URLRequest(url: url)
            webView.loadRequest(request)
        }
    }
}
Mukesh Shakya
  • 425
  • 3
  • 9
  • Thanks for taking the time to reply. Seems like UIWebView is deprecated now, and I wasn't able to get the code to work with WKWebView. But thank you all the same. – revlis Jun 13 '19 at 07:00
0

If you want to open website in BrowserController then after you get url from textfield from ViewController use this code in your BrowserController

import UIKit
import SafariServices

class BrowserController: UIViewController {
//@IBOutlet weak var label: UILabel!

var urlString = String()

override func viewDidLoad() {
    super.viewDidLoad()

    label.text = urlString

    guard let url = URL(string: urlString ?? "") else { return }
    let svc = SFSafariViewController(url: url)
    present(svc, animated: false, completion: nil)
}
}

This is the easiest way to open browser from the app.

Habin Lama
  • 529
  • 5
  • 19
  • I wasn't actually able to get this one to work either, but thanks for taking the time to post. – revlis Jun 13 '19 at 07:01
0

In case you are thinking of having a list of table where the click could lead to the website, check out this tutorial that I write.

https://daddycoding.com/2017/10/17/ios-tutorials-triggering-tableviewcell-visit-site/

Kelvin Tan
  • 327
  • 3
  • 15