skytorner
skytorner

Reputation: 415

Xcode Opening a WebView in another ViewController

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

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 !

Upvotes: 1

Views: 3414

Answers (2)

Lucas Palaian
Lucas Palaian

Reputation: 374

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.

Upvotes: 2

DiegoQ
DiegoQ

Reputation: 1116

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

Upvotes: 0

Related Questions