Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load URL in UIWebView in Swift?

Tags:

swift

ios8

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?

like image 421
User Avatar asked Aug 31 '14 23:08

User


People also ask

How do I load a URL in WKWebView swift 5?

Loading local content WKWebView can load any HTML stored in your app bundle using its loadFileURL() method. You should provide this with a URL to some HTML file that you know is in your bundle, along with another URL that stores any other files you want to allow the web view to read. That url.


1 Answers

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) 
like image 147
Mick MacCallum Avatar answered Sep 21 '22 13:09

Mick MacCallum