I have the following code:
UIWebView.loadRequest(NSURLRequest(URL: NSURL(string: \"google.ca\")))
I am getting the following error:
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!))
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/
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)
}
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
@IBOutlet weak var webview: UIWebView!
webview.loadRequest(URLRequest(url: URL(string: "https://www.yourvideo.com")!))
Try This
let url = NSURL (string: "http://www.apple.com");
myWebView.loadRequest(NSURLRequest(URL: url!));