I try to show a full responsive bootstrap website in a webView app. The app should just load my website as if I load the website in the browser of the smartphone/tablet.
However, the app looks like this on tablets:
How can I make the app full responsive?
Whole Code:
ViewController.swift
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// 1 The webView loads the url using an URLRequest object.
let url = URL(string: "https://www.blizz-z.de/")!
webView.load(URLRequest(url: url))
// 2 A refresh item is added to the toolbar which will refresh the current webpage.
let refresh = UIBarButtonItem(
barButtonSystemItem: .refresh,
target: webView,
action: #selector(webView.reload)
)
toolbarItems = [refresh]
navigationController?.isToolbarHidden = true
navigationController?.isNavigationBarHidden = true
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
let color = UIColor.black;
self.view.backgroundColor = color
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}
There is no problem in your code.
Switch to a target settings in Xcode, on a tab "General" in a section "Deployment target" switch "Devices" to "Universal".
Also add "Launch Screen.storyboard" if you did not add it.
you should add constraint for the webView, and the constant of top/bottom/left/right constraint is 0 to superView, not safe area
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.setupWebview()
let refresh = UIBarButtonItem(
barButtonSystemItem: .refresh,
target: webView,
action: #selector(webView.reload)
)
toolbarItems = [refresh]
navigationController?.isToolbarHidden = true
navigationController?.isNavigationBarHidden = true
}
func setupWebview(){
self.webView.frame = UIScreen.main.bounds
self.webView.navigationDelegate = self
self.view.backgroundColor = UIColor.black
let url = URL(string: "https://www.blizz-z.de/")!
self.webView.load(URLRequest(url: url))
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}
Try this... (Note: Not a copy pasted code)
Related
So I have a view controller that sets a WKWebView as its view and it loads a PDF from a website. But the navigationBar cuts off the top of the PDF, but it doesn't cut it off when a website is loaded. Anyone know how to fix it?
class WebView: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
var urlString: String?
var pdf: NSData?
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.largeTitleDisplayMode = .never
let url = URL(string: urlString ?? "https://www.cpp.edu")!
webView.load(URLRequest(url: url))
pdf = NSData(contentsOf: url)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
}
#objc func shareTapped() {
let view = UIActivityViewController(activityItems: [pdf!], applicationActivities: nil)
present(view, animated: true)
}
}
I'm trying to show a website using the WKWebView, it works fine as for the internet work.
problem is it is loading the site twice (it seems like it's going to another ViewController).
here is my code:
#IBOutlet weak var myWebView: WKWebView!
override func loadView() {
myWebView = WKWebView()
myWebView.navigationDelegate = self as? WKNavigationDelegate
view = myWebView
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = false
let url = URL(string: "https://google.com")!
myWebView.load(URLRequest(url: url))
}
I have hooked up an activityIndicator in the storyboard and created a webView programmatically. However when I load the VC, the program crashes complaining that the activityIndicator is nil. Why is this? I confirmed that the outlet is connected properly.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.google.com/")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
activityIndicator.startAnimating()
print("loadingwebpage")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("didfinishloadingwebpage")
activityIndicator.stopAnimating()
}
}
You have this line :
view = webView
this mean you assign created webView to viewController's view. thats why your indicator destroy and cause crash.
add webView to viewcontroller's subview and crash will gone. change:
webView.frame = self.view.bounds
self.view.addSubview(webView)
How can I refresh/load all the contents of my .html page before the webview is opening/showing?
This is my code:
class ContactViewController: UIViewController {
#IBOutlet weak var AboutWebView: WKWebView!
let url = URLAddress()
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.myfakeurl.com/about.html")
let request = URLRequest(url: url!)
AboutWebView.load(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
How can i adjust that with my code?
Just hide the web view until it's fully loaded and display it in webView(_:didFinish:).
Note that you should conform your view controller to WKNavigationDelegate.
class ContactViewController: UIViewController, WKNavigationDelegate {
override func viewDidLoad() {
[...]
aboutWebView.navigationDelegate = self
aboutWebView.isHidden = true
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.isHidden = false
}
}
Using swift with Xcode8
Below is my view controller.swift
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var XWebview: WKWebView!
var activity = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
override func loadView() {
super.loadView()
self.XWebview = WKWebView()
self.XWebview.navigationDelegate = self
self.view = self.XWebview
}
override func viewDidLoad() {
XWebview.navigationDelegate = self
activity.center = self.view.center
activity.color = UIColor.gray
XWebview.addSubview(activity)
super.viewDidLoad()
let XURL = NSURL(string: "http://www.yahoo.com")
let XURLRequest = NSURLRequest(url: XURL! as URL)
XWebview.load(XURLRequest as URLRequest)
}
override var prefersStatusBarHidden: Bool {
return true
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
activity.startAnimating()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
activity.stopAnimating()
}
}
I did not see any indicator when load web view?
Did I do something wrong?
You are using WKWebView and implemented the UIWebView delegate. It will not trigger. You have to implement WKWebView protocol methods.
Implement the following WKNavigationDelegate protocol method to identify start loading the view. This will be triggered when a main frame navigation starts.
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)
Make sure your activity indicator color with not same asWKWebView background color. Make it grey
Use main queue to show/hide activity indicator.
DispatchQueue.main.async {
activity.startAnimating()
}
DispatchQueue.main.async {
activity.stopAnimating()
}
Use grey color for activity indicator if your web view is white color.
var activity = UIActivityIndicatorView(activityIndicatorStyle: .gray)