How to load URL in UIWebView in Swift?

2020-01-26 04:08发布

问题:

I have the following code:

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

I am getting the following error:

'NSURLRequest' is not convertible to UIWebView.

Any idea what the problem is?

回答1:

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")!))   


回答2:

UIWebView in Swift

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()
        let url = URL (string: "url here")
        let requestObj = URLRequest(url: url!)
        webView.loadRequest(requestObj)
    // Do any additional setup after loading the view.
}

/////////////////////////////////////////////////////////////////////// if you want to use webkit

@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
    super.viewDidLoad()

    let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.webView.frame.size.height))
    self.view.addSubview(webView)
    let url = URL(string: "your URL")
    webView.load(URLRequest(url: url!))`


回答3:

Try this:

  1. Add UIWebView to View.

  2. Connect UIWebview outlet using assistant editor and name your "webview".

  3. UIWebView Load URL.

    @IBOutlet weak var webView: UIWebView!
    
    override func viewDidLoad() {
       super.viewDidLoad()
        // Your webView code goes here
       let url = URL(string: "https://www.example.com")
       let requestObj = URLRequest(url: url! as URL)
       webView.load(requestObj)
    }
    

And run the app!!



回答4:

Swift 3 doesn't use NS prefix anymore on URL and URLRequest, so the updated code would be:

let url = URL(string: "your_url_here")
yourWebView.loadRequest(URLRequest(url: url!))


回答5:

In Swift 3 you can do it like this:

@IBOutlet weak var webview: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Web view load
    let url = URL (string: "http://www.example.com/")
    let request = URLRequest(url: url!)
    webview.loadRequest(request)
}

Remember to add permission at Info.plist, add the following values:



回答6:

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)

        }


回答7:

Swift 3

@IBOutlet weak var webview: UIWebView!
webview.loadRequest(URLRequest(url: URL(string: "https://www.yourvideo.com")!))


回答8:

Loading URL to WebView is very easy. Just create a WebView in your storyboard and then you can use the following code to load url.

    let url = NSURL (string: "https://www.simplifiedios.net");
    let request = NSURLRequest(URL: url!);
    webView.loadRequest(request);

As simple as that only 3 lines of codes :)

Ref: UIWebView Example



回答9:

UIWebView loadRequest: Create NSURLRequest using NSURL object, then passing the request to uiwebview it will load the requested URL into the web view.

 override func viewDidLoad() {
     super.viewDidLoad()
     // Do any additional setup after loading the view, typically from a nib.
     let url = NSURL (string: "http://www.google.com");
     let requestObj = NSURLRequest(URL: url!);
     myWebView.loadRequest(requestObj);
     self.view.addSubview(myWebView)
  }

For more reference:

http://sourcefreeze.com/uiwebview-example-using-swift-in-ios/



回答10:

Used Webview in Swift Language

let url = URL(string: "http://example.com")
   webview.loadRequest(URLRequest(url: url!))


回答11:

Easy,Tested and working 100%

Import webkit :

import WebKit

Assign IBOutlet to webview:

var webView : WKWebView!

set delegate:

class ViewController: UIViewController , WKNavigationDelegate{

Write code on viewDidLoad():

// loading URL :
let myBlog = "https://stackoverflow.com/users/4600136/mr-javed-multani?tab=profile"
let url = NSURL(string: myBlog)
    let request = NSURLRequest(url: url! as URL)

    // init and load request in webview.
    webView = WKWebView(frame: self.view.frame)
    webView.navigationDelegate = self
    webView.load(request as URLRequest)
    self.view.addSubview(webView)
    self.view.sendSubview(toBack: webView)

Write delegate methods:

//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")
}

look like:



回答12:

For swift 4.2, 5+

@IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
       super.viewDidLoad()
           //Func Gose Here 
          let url = URL(string: link)
          let requestObj = URLRequest(url: url! as URL)
           webView.loadRequest(requestObj)

       }


回答13:

For Swift 3.1 and above

let url = NSURL (string: "Your Url")
let requestObj = NSURLRequest(url: url as! URL);
YourWebViewName.loadRequest(requestObj as URLRequest)


回答14:

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)
}}


回答15:

In Swift 4 or 4.2 You can use like:

  1. Add WKWebView & connect to you view comtroller.
  2. Your view is like bellow:

     import UIKit
     import WebKit
    
    class ViewController: UIViewController {
    
       @IBOutlet weak var wkwebview: WKWebView!
    
        override func viewDidLoad() {
           super.viewDidLoad()
    
          let request = URLRequest(url: URL(string: "**your URL**")!)        
          wkwebview?.load(request)
    
      }
    
    }
    
  3. Allow Allow Arbitrary Loads true info.plist

      <key>NSAppTransportSecurity</key>
         <dict>
           <key>Allow Arbitrary Loads</key>
              <true/>
        </dict>
    

    Note info.plist will look like bellow



回答16:

import UIKit

class ViewController: UIViewController{

@IBOutlet weak var webView: UIWebView!



override func viewDidLoad() {
   super.viewDidLoad()
       //Func Gose Here 
      loadUrl()

   } 
 func loadUrl(){

   let url = URL(string: "Url which you want to load(www.google.com)")
   let requestObj = URLRequest(url: url! as URL)
   webView.load(requestObj)
 }

}


回答17:

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.



回答18:

Try This

let url = NSURL (string: "http://www.apple.com");
myWebView.loadRequest(NSURLRequest(URL: url!));


标签: swift ios8