iOS Swift WKWebKit with MBCircularProgressBarView - ios

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
}

Related

How to make an activity indecator with a WKwebView in Swift

I am trying to make an iOS app with Swift and I have sorted out the webView which works perfectly, however now i'd like to implement an activity indicator. It displays properly and is animated, however when the page is loaded, the activity indicator does not disappear.
This is my code:
import UIKit
import WebKit
class FirstViewController: UIViewController {
#IBOutlet var webview: WKWebView!
// #IBOutlet var bg: UIView!
// #IBOutlet var logo: UIImageView!
#IBOutlet var ActInd: UIActivityIndicatorView!
// #IBOutlet var LoadingLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.google.com")
let request = URLRequest(url: url!)
webview.load(request)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidStartLoad(_ : WKWebView) {
ActInd.startAnimating()
//LoadingLabel.isHidden = true
//logo.isHidden = true
//bg.isHidden = true
}
func webViewDidFinishLoad(_ : WKWebView){
ActInd.stopAnimating()
// LoadingLabel.isHidden = true
//logo.isHidden = true
//bg.isHidden=true
}
}
I'd appreciate it enormously if someone could help me get the activity indicator to disappear.
Thank you in advance
Try setting
ActInd.hidesWhenStopped = true // In viewDidLoad
Conform to WKNavigationDelegate, and you can track webView processes..
The methods of the WKNavigationDelegate protocol help you implement custom behaviors that are triggered during a web view's process of accepting, loading, and completing a navigation request.
There are two methods that you can use :
func webView(WKWebView, didStartProvisionalNavigation: WKNavigation!)
Called when web content begins to load in a web view.
Here you can show the activity indicator
func webView(WKWebView, didFinish: WKNavigation!)
Called when the navigation is complete.
For more info check WKNavigationDelegate documentation
So here are the steps :
1- Conform to WKNavigationDelegate.
2- Assign your view controller to be your web view’s navigationDelegate.
3- Implement didStartProvisionalNavigation and didFinish methods
Example :
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webview: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webview.navigationDelegate = self
/*
....
*/
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// hide/stop indicator
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
//show your activity
}
}
On view did load just start your indicator and on webViewDidFinishLoad just hide that loader.
The methods won't ever be called.
Because, you haven't set your ViewController as NavigationDelegate. It needs to conform to the WKNavigationDelegate. You can then set the delegate in the viewDidLoad() function:
webview.navigationDelegate = self
The 2 methods you're calling also don't seem to exist. These are the equivalent delegate methods: webView(:didCommit:) and webView(:didFinish:)
You can set "Hides when stopped" and "Animated" in the storyboard or do it programatically as it is explained in this answer.
You should also take a look at the API Design guidelines explaining how code should be written. Especially you should:
Follow case conventions. Names of types and protocols are UpperCamelCase. Everything else is lowerCamelCase.

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

How do I correctly disable link detection in UIWebView?

I Have a UIWebView strictly for viewing PDF's, and I don't want any link detection in the view, Like this:
So I added the following code to negate the detection of links and the presentation of that view:
override func viewDidLoad() {
super.viewDidLoad()
myWebView.dataDetectorTypes = UIDataDetectorTypes.None
}
However it still picks up links, and presents that modal view. How do I correctly implement code to stop this?
Here is my updated code:
class PdfViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var myWebView: UIWebView!
var contentUrlPassedOn: String!
var allowLoad = true
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self
let url: NSURL! = NSURL(string: contentUrlPassedOn)
myWebView.loadRequest(NSURLRequest(URL: url))
myWebView.dataDetectorTypes = UIDataDetectorTypes.None
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return allowLoad
}
func webViewDidStartLoad(webView: UIWebView) {
var hasFinishedLoading = false
updateProgress()
}
func webViewDidFinishLoad(webView: UIWebView) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))),
dispatch_get_main_queue()) {
[weak self] in
if let _self = self {
_self.hasFinishedLoading = true
self!.allowLoad = false
}
}
}
If your webview was added by the storyboard, You can disable this in the attributes inspector.
[1
It's an objective-c code, but u can do the same in Swift. It will fix the interaction with links. The con is that they will be still detected.
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if (inType == UIWebViewNavigationTypeLinkClicked) {
return NO;
}
return YES;
}
Finely Solve the problem for you,
Working Demo
Cannot disable long press link menu, Which is Bug in iOS, That's solve in iOS 8.2.
According to bug, when press long on link then its open Action Sheet, Now in iOS 8.2 its solve as below.
Load PDF using WKWebView as below.
import UIKit
import WebKit
class ViewController: UIViewController , WKUIDelegate {
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
super.viewDidLoad()
var url = NSURL(string:"http://www.nursing.umich.edu/studentresources/documentation/CreatingBookmarksPDFdocsV7.pdf")
var req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req)
}
}
Now you Click on your long Press link then its doesn't open ActionSheet PopUp.
Enjoy..!!
Output :

Resources