Load/Refresh WKWebView before opening/loading it - ios

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

Related

iOS Swift WKWebKit with MBCircularProgressBarView

before posting this I was looking on the internet how to implement from cocoa pods MBCircularProgressBarView in iOS Swift WKWebKit to track progress of loading website.
I have tried with combination of some other progress bar codes but it didn't work.
I have tried to implement self.progressView.value = 0 in viewDidLoad
and
UIView.animate(withDuration: 1.0){
self.progressView.value = 100
}
in
didFinish navigation
which shows circular bar and animate but doesn't show proper loading progress.
Any idea how to make this work.
Make a class class WebpageViewController: UIViewController, UIWebViewDelegate containing:
#IBOutlet weak var activitySpinner: UIActivityIndicatorView!
#IBOutlet weak var activityLabel: UILabel!
override func viewDidAppear(_ animated: Bool)
{
activitySpinner.tintColor = R.color.YumaRed
activitySpinner.hidesWhenStopped = true
activityLabel.text = "Loading..."
webView.loadRequest(URLRequest(url: URL(string: "http://...")!))
}
func webViewDidStartLoad(_ webView: UIWebView)
{
activitySpinner.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView)
{
activitySpinner.stopAnimating()
activityLabel.isHidden = true
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
{
activitySpinner.stopAnimating()
}
override func viewDidLoad()
{
super.viewDidLoad()
webView.delegate = self
}

UIActivityIndicatorView nil?

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)

Why my indicator is not working when loading web view?

Using swift3 with Xcode8
Below is mu view controller.swift
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var YahooWebview: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
YahooWebview.delegate = self
super.viewDidLoad()
let YURL = URL(string: "http://www.yahoo.com")
let YURLRequest = URLRequest(url: YURL!)
YahooWebview.loadRequest(YURLRequest)
}
func webViewDidStartLoad(YahooWebview: UIWebView) {
print("show indicator")
activity.startAnimating()
}
func webViewDidFinishLoad(YahooWebview: UIWebView) {
activity.stopAnimating()
}
}
From my log in Xcode, it print "show indicator" then stop and then show something like the picture below.
Seems like something wrong with code below
activity.startAnimating()
Can anyone help?
Try below code for load site in web view
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var YahooWebview: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let YURL = URL(string: "http://www.yahoo.com")
YahooWebview.delegate = self
let YURLRequest = URLRequest(url: YURL!)
YahooWebview.loadRequest(YURLRequest)
}
func webViewDidStartLoad(_ webView: UIWebView) {
activity.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView) {
activity.stopAnimating()
}
}

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)

WebView cannot scroll from top to bottom of the webpage

I have loaded a webpage to xcode WebView. But only the upper portion of the page is loaded. I can not scroll down to the bottom of the page. Same fact for pdf. Only upper 2 pages can be scrolled. What can i do ?Here is my code.
Thanks in advance.
import UIKit
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
var URL = NSURL(string: "http://www.archetapp.com")
webView.loadRequest(NSURLRequest(URL: URL!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//for pdf
import UIKit
class ViewController: UIViewController {
#IBOutlet var webViews: UIWebView!
var path = ""
override func viewDidLoad() {
super.viewDidLoad()
path = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
webViews.scalesPageToFit = true
webViews.scrollView.scrollEnabled = true
webViews.userInteractionEnabled = true
self.webViews.loadRequest(NSURLRequest(URL: url!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Try this!
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView : UIWebView
override func viewDidLoad() {
super.viewDidLoad()
//load initial
path = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
var req = NSURLRequest(URL : url)
webView.delegate = self // <---
webView.loadRequest(req)
}
func webViewDidStartLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = true
println("webViewDidStartLoad")
}
func webViewDidFinishLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = false
webViews.scalesPageToFit = true
webViews.scrollView.scrollEnabled = true
webViews.userInteractionEnabled = true
println("webViewDidFinishLoad")
}
}
I had the same issue just today and at least in my case it was caused by having the "Scale Pages to Fit" option checked in the WebView properties. I'm assuming Carlos' reply regarding the zoom scale fixes it anyways but I didn't need the option enabled in the first place so that was my easy fix.

Resources