I want the user to press a button and in the WebView there should open a specific pdf document. I have tried several tutorials but they are all outdated (I think). My code so far:
import UIKit
class ViewController: UIViewController {
#IBAction func show(_ sender: Any) {
let url = NSBundle.mainBundle().URLForResource("file", withExtension: "pdf")
if let url = url {
let webView = UIWebView(frame: self.view.frame)
let urlRequest = NSURLRequest(URL: url)
webView.loadRequest(urlRequest)
view.addSubview(webView)
}
}
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
But NSBundle doesn't exist. It has been renamed to Bundle. But even after applying this fix suggestion there are many many other errors and when I fix them all then still nothing works. So there has to be a difference between Swift 1 (or 2) and Swift 3 in showing a pdf document.
Or do I have missed something?
Try to this code for load local PDF inside webView: -
NSBundle now in Swift 3 version as a Bundle
Swift 3
if let pdf = Bundle.main.url(forResource: "file", withExtension: "pdf", subdirectory: nil, localization: nil) {
let req = NSURLRequest(url: pdf)
let webView = UIWebView(frame: CGRect(x:0,y:0,width:self.view.frame.size.width,height: self.view.frame.size.height-40)) //Adjust view area here
webView.loadRequest(req as URLRequest)
self.view.addSubview(webView)
}
I try to add address in several ways, but it's not working all the time. Anybody know the right code?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var Dziennik: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.google.com")!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
let url = URL (string: "http://google.com")
let requestObj = URLRequest(URL: url!);
Dziennik.loadRequest(requestObj)
Just change this:
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.google.com")!))
for this:
Dziennik.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.google.com")!))
The issue is the name of the property.
i try to use WKWebView, but it don' work.
here is my code
import UIKit
import WebKit
class WebviewVC: UIViewController, WKNavigationDelegate {
var webview: WKWebView?
// MARK: - ********** Lifecycle **********
override func viewDidLoad() {
super.viewDidLoad()
webview = WKWebView(frame: view.bounds)
view.addSubview(webview!)
webview!.navigationDelegate = self
loadUrl("http://www.google.com")
webview!.allowsBackForwardNavigationGestures = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - ********** load url ***********
func loadUrl(urlStr: String) {
let url = NSURL(fileURLWithPath: urlStr)
let requestObj = NSURLRequest(URL: url)
webview!.loadRequest(requestObj)
}
}
i have seted the App Transport Security Settings -> Allow Arbitrary Loads to YES. but the webView also don't show the website.
pls help!
What is this:
let url = NSURL(fileURLWithPath: urlStr)
you should use:
let url = NSURL(string: urlStr)
I have simple example where one webView is there which loads one link when the app loads successfully the Debug navigator looks like:
App takes 73.5 MB of Memory after that when I scroll down into simulator continuously memory increase too much like :
can anybody explain me why it is taking too much memory?
Or tell me if it causes any problem?
here is my code for this:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = "https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_456"
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!)
webView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
you can use WKWebView instead of UIWebView which will not take more memory, here is an example how to use it in swift 3:
import UIKit
import WebKit
class ViewController: UIViewController
{
var webView: WKWebView!
override func viewDidload() {
super.viewDidload()
let url = "https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_456"
let requestURL = URL(string:url)
let request = URLRequest(url: requestURL!)
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(webView)
webView.load(request)
}
}
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")!))
Swift 5
webviewInstance.load(NSURLRequest(url: NSURL(string: "google.ca")! as URL) as URLRequest)
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:
In Swift 4 or 4.2 You can use like:
Add WKWebView & connect to you view comtroller.
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
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")!))
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:
in swift 5
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
}
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:
https://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!))
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)
}}
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!));