How to load URL in UIWebView in Swift?

前端 未结 18 1415
南旧
南旧 2020-11-29 00:58

I have the following code:

UIWebView.loadRequest(NSURLRequest(URL: NSURL(string: \"google.ca\")))

I am getting the following error:

相关标签:
18条回答
  • 2020-11-29 00:59

    loadRequest: is an instance method, not a class method. You should be attempting to call this method with an instance of UIWebview as the receiver, not the class itself.

    webviewInstance.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")!))
    

    However, as @radex correctly points out below, you can also take advantage of currying to call the function like this:

    UIWebView.loadRequest(webviewInstance)(NSURLRequest(URL: NSURL(string: "google.ca")!))   
    

    Swift 5

    webviewInstance.load(NSURLRequest(url: NSURL(string: "google.ca")! as URL) as URLRequest)
    
    0 讨论(0)
  • 2020-11-29 00:59

    You can load a page like this :

    let url: URL = URL(string:"https://google.com")!
    webView.loadRequest(URLRequest.init(url: url))
    

    Or the one-line approach :

    webView.loadRequest(URLRequest.init(url: URL(string: "https://google.com")!))
    

    webView is your outlet var.

    0 讨论(0)
  • 2020-11-29 01:02

    Swift 3 - Xcode 8.1

     @IBOutlet weak var myWebView: UIWebView!
    
        override func viewDidLoad() {
                super.viewDidLoad()
    
                let url = URL (string: "https://ir.linkedin.com/in/razipour1993")
                let requestObj = URLRequest(url: url!)
                myWebView.loadRequest(requestObj)
    
            }
    
    0 讨论(0)
  • 2020-11-29 01:04

    Swift 4 Update Creating a WebView programatically.

    import UIKit
    import WebKit
    class ViewController: UIViewController, WKUIDelegate {
    
    var webView: WKWebView!
    
    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let myURL = URL(string: "https://www.apple.com")
        let myRequest = URLRequest(url: myURL!)
        webView.loadRequest(myRequest)
    }}
    
    0 讨论(0)
  • 2020-11-29 01:05

    For Swift 3.1 and above

    let url = NSURL (string: "Your Url")
    let requestObj = NSURLRequest(url: url as! URL);
    YourWebViewName.loadRequest(requestObj as URLRequest)
    
    0 讨论(0)
  • 2020-11-29 01:06

    Used Webview in Swift Language

    let url = URL(string: "http://example.com")
       webview.loadRequest(URLRequest(url: url!))
    
    0 讨论(0)
提交回复
热议问题