UIActivityIndicatorView nil? - ios

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)

Related

iOS webView is not using whole screen

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)

WKWebView - buttons not visible in full screen mode

I have a ViewController with WebKit and two buttons above it. I want WebKit to be fullscreen and buttons to be above it.
This is code of my ViewController:
#IBOutlet weak var webview: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// here I am loading URLRequest
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func loadView() {
webview = WKWebView()
webview.navigationDelegate = self
view = webview
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
showLoading()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
hideLoading()
}
This is what my storyboard looks like:
Methods showLoading() and hideLoading() are handling loading popup.
With this code buttons are not visible. When I remove function loadView() buttons are visible but methods showLoading() and hideLoading() are never called.
How can I achieve mentioned behavior: fullscreen webview with buttons and functional methods (showLoading() and hideLoading())
what happens in your current code is you arbitrarily replace the current view with your webview instance and that directly removes the current view's subviews (=your buttons) as well.
you can either
a)
insert the webview directly:
override func loadView() {
webview = WKWebView()
webview.navigationDelegate = self
view.insertSubview(webview, at: 0)
webview.frame = view.bounds
}
or
b)
add the WKWebView to your view in the Interface Builder under your buttons then connect the outlet and the delegate(s) from IB and you can remove your loadView() implementation entirely.

Disabling zooming on webview

I've searched around, but I couldn't find how to disable zooming in iOS.
I have this in my viewDidLoad() but it doesn't do anything.
webView.scrollView.isMultipleTouchEnabled = false;
Any ideas?
You can remove the pinchGestureRecognizer from scrollView in delegate method didFinish navigation by conforming your ViewController to WKNavigationDelegate as below,
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// TODO: Initialize webView before setting the delegate
webView.navigationDelegate = self
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if let pinchGesture = webView.scrollView.pinchGestureRecognizer {
webView.scrollView.removeGestureRecognizer(pinchGesture)
}
}
}

Load/Refresh WKWebView before opening/loading it

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
}
}

Why webViewDidStartLoad is not calling?

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)

Resources