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?
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?
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")!))
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!))`
Try this:
Add UIWebView to View.
Connect UIWebview outlet using assistant editor and name your "webview".
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!!
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!))
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:
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)
}
Swift 3
@IBOutlet weak var webview: UIWebView!
webview.loadRequest(URLRequest(url: URL(string: "https://www.yourvideo.com")!))
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
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/
Used Webview in Swift Language
let url = URL(string: "http://example.com")
webview.loadRequest(URLRequest(url: url!))
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:
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)
}
For Swift 3.1 and above
let url = NSURL (string: "Your Url")
let requestObj = NSURLRequest(url: url as! URL);
YourWebViewName.loadRequest(requestObj as URLRequest)
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)
}}
In Swift 4 or 4.2 You can use like:
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)
}
}
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
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)
}
}
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.
Try This
let url = NSURL (string: "http://www.apple.com");
myWebView.loadRequest(NSURLRequest(URL: url!));