1

I'm trying to make an app with a button which launch a webview.

I've followed many tutorial and read differents topic about the subject but I cant get it working : I'm getting this message when I test my code :

    "Cannot call value of non-function type UIWebView!"

Here's the steps I did until now

  • Adding a button in the principal view Controller
  • Creating an another view Controller named 'WebViewController'
  • Adding a segue to link the button to WebViewController
  • Creating a new Cocoa Touch Class file 'WebViewController'
  • Setting the WebViewController custom class with the WebViewController class
  • Adding a webView in the WebViewController ViewController named 'myWebView'

Here's the WebViewController class (in which I got the error when I run the project)

  import UIKit

  class WebViewController: UIViewController{

  @IBOutlet weak var myWebView: UIWebView!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    //define url
    let url = NSURL (string: "http://www.my-url.com")
    //request
    let req = NSURLRequest(url: url as! URL)
    //load request into the webview
    myWebview(req as URLRequest) //error happens here : 
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

Here's a screenshot (picture talks more than long text, right =) enter image description here

Thanks !

skytorner
  • 405
  • 2
  • 8
  • 19

2 Answers2

2

You can use SFSafariViewController:

import SafariServices

let url = URL(string: "https://www.google.com")
let safariVC: SFSafariViewController = SFSafariViewController(url: url)

self.present(safariVC, animated: true, completion: nil)

I used swift 3 syntax. That code opens a Safari Web view and you dont need to create segues and view controlles in storyboard.

Lucas Palaian
  • 374
  • 2
  • 12
0

Try to use:

  let url = NSURL (string: "https://google.com")
  let request = NSURLRequest(url: url as! URL)
  self. myWebView.loadRequest(request as URLRequest)

This code works for me

DiegoQ
  • 1,114
  • 11
  • 20