I'm trying to set up an activity indicator on my TableView where is my news feed is displayed. So I want that idicator to appear whenever load starts and disappear when it ends.
I know there is a function for webView like this:
func webViewDidFinishLoad(webView: UIWebView) {
activityView.removeFromSuperview()
}
func webViewDidStartLoad(webView: UIWebView) {
self.view.addSubview(activityView)
}
But it won't work for Table View obviously. Can someone suggest a solution?
I've tried to apply an activity indicator both in the center of my screen and in my Status bar but to no succsess.
I can add full code of my TableView controller if needed.
Thank you!
EDIT
override func viewDidLoad() {
super.viewDidLoad()
// Cell height.
self.tableView.rowHeight = 70
self.tableView.dataSource = self
self.tableView.delegate = self
url = NSURL(string: "http---")!
//url = NSURL(string: "http---")!
//url = NSURL(string: "http---")!
loadRss(url);
To show the small activity indicator in the status bar
func webViewDidFinishLoad(webView: UIWebView) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func webViewDidStartLoad(webView: UIWebView) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
show the small activity
This is links you can see the answer
Related
I have a tabbed app which loads separate internal file WKwebviews. I now need to have the WKwebview refresh when a tab is selected.
I think I need to add the required code in the viewWillAppear, but on trying to have some code on this method nothing works.
Does anyone have any suggestions as to how I can achieve this
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
webView.configuration.userContentController.add(self, name: "jsHandler")
let bundleURL = Bundle.main.resourceURL!.absoluteURL
let html = bundleURL.appendingPathComponent("main/index.html") //Loads internal HTML files
webView.loadFileURL(html, allowingReadAccessTo:bundleURL)
webView!.uiDelegate = self
webView.allowsBackForwardNavigationGestures = true //Allows 'safari' style gestures swipe back etc
}
override func viewWillAppear(_ animated: Bool) {
//Nothing
}
Update:
To resolve this, I added the above code into the viewDidAppear method rather than the viewDidLoad. This has resolved the problem.
The JS message handler still needs to be in the viewDidLoad.
Example:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
webView.configuration.userContentController.add(self, name: "jsHandler")
}
override func viewDidAppear(_ animated: Bool) {
//webView.configuration.userContentController.add(self, name: "jsHandler")
let bundleURL = Bundle.main.resourceURL!.absoluteURL
let html = bundleURL.appendingPathComponent("index.html") //Loads internal HTML files
webView.loadFileURL(html, allowingReadAccessTo:bundleURL)
webView!.uiDelegate = self
webView.allowsBackForwardNavigationGestures = true //Allows 'safari' style gestures swipe back etc
}
Try something like this on each tab action:
func reloadTabAction() {
if let url = webView.url {
webView.reload()
} else {
webView.load(URLRequest(url: originalURL))
}
}
We have reload property for WKWebView. So you can directly call the method.
You can call the method while tapped the tabView
Try the below code,
webView.reload()
I'm loading a pdf in WebView from my server with this code :
webView = UIWebView(frame: CGRectMake(0, y, screenSize.width, screenSize.height-y))
let url : NSURL! = NSURL(string: urlFile)
webView?.loadRequest(NSURLRequest(URL: url))
webView?.opaque = false;
webView?.backgroundColor = UIColor.clearColor()
webView?.scalesPageToFit = true;
self.view.addSubview(webView!)
This code works but how can i receive an event "onPageLoad"?
sorry for bad english, i'm italian(:
You need to implement UIWebViewDelegate like this and use webViewDidFinishLoad to know the page is loaded successfully, for that set the webView delegate with your viewController like this, and implement the webViewDidFinishLoad method inside your viewController like below example.
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
webView = UIWebView(frame: CGRectMake(0, y, screenSize.width, screenSize.height-y))
let url : NSURL! = NSURL(string: urlFile)
webView?.loadRequest(NSURLRequest(URL: url))
webView?.opaque = false;
webView?.backgroundColor = UIColor.clearColor()
webView?.scalesPageToFit = true;
webView?.delegate = self// Add this line to set the delegate of webView
self.view.addSubview(webView!)
}
func webViewDidFinishLoad(webView : UIWebView) {
//Page is loaded do what you want
}
}
In case someone is looking solution for WKWebView, implement WKNavigationDelegate:
add webView.navigationDelegate = self
and then use:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
}
After webView?.loadRequest(NSURLRequest(URL: url)) this, set the webView to self (Write the below line).
webView.delegate = self
Implement the webview delegate methods.
You will get callback in func webViewDidFinishLoad(_ webView: UIWebView) on completing the page load.
func webViewDidFinishLoad(webView: UIWebView) {
//handle the callback here when page load completes
}
Just use effective with ;
func webViewDidFinishLoad(webView : UIWebView) {
// Here your loaded codes.
}
I'm loading html string by WebView in my app. Current issue is that when my WebView load content that contains another link and when user click on that link, new view is not opening in my app - it opens in default browser. How can I open that link in WebView? Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
self.edgesForExtendedLayout = UIRectEdge.None
SVProgressHUD.show()
//EZLoadingActivity.show("أرجو الإنتظار", disableUI: true)
self.automaticallyAdjustsScrollViewInsets = false
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor()
self.navigationController!.navigationBar.barStyle = UIBarStyle.Black
self.navigationController!.navigationBar.tintColor = UIColor.whiteColor()
let logo = UIImage(named: "toolbar_icon3")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
myweb.loadHTMLString(webviewurl, baseURL: nil)
}
Android allows to do it with:
public boolean shouldOverrideUrlLoading(WebView view, String url)
But on iOS how can we do this?
You could try creating a url request for you UIWebView.
yourWebView.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")!))
If you would like to do something while the web view is opening, you could use the UIWebViewDelegate statement.
In your class:
class ViewController: UIViewController, UIWebViewDelegate {
Then,
yourWebView.delegate = self
And finally, you can use:
func webViewDidStartLoad(webView: UIWebView) {
//do something while the web view is loading!
}
For more information on UIWebViewDelegate, visit Apple's Documentation.
I want to make that when tableView row tapped and opens the webView screen my progressBar appears with the value 0.3. Later it progress +0.1 and when it loaded it becomes 1 and hide it.
What I do:
progressBar.progress = 0.3
myWebView.loadHTMLString(body, baseURL: nil)
progressBar.progress = 1
It loads immediately full progress on tap. How can I do it? Must I turn off async?
UPDATE
I did also
func webViewDidStartLoad(webView: UIWebView) {
progressBar.progress = 0.2
}
func webViewDidFinishLoad(webView: UIWebView) {
progressBar.progress = 1
}
but it doesn't work. It stays in some position
UPDATE 2
They did not call yet. I wrote println inside of these functions and nothing printed
You better see this tutorial: http://www.ioscreator.com/tutorials/progress-view-tutorial-in-ios8-with-swift
Well, your mistake in the code is:
When 'webViewDidStartLoad' will be called then progress bar will increase to 0.2%, that's fine! Because its the initial point. But when you are going to increase to 0.2 - 0.4 or 0.2 - 0.3, for that you have to run as an Async task. So, please look at the tutorial and increase the counter accordingly.
Hope you got it!
I just drag from my webView and delegate it with my viewController and it works now!
Try this out
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://topdigital.agency")
let request = NSURLRequest(url: url as! URL)
webView.loadRequest(request as URLRequest)
webView.delegate=self
}
func webViewDidStartLoad(_ webView: UIWebView) {
self.progressView.setProgress(0.1, animated: false)
}
func webViewDidFinishLoad(_ webView: UIWebView) {
self.progressView.setProgress(1.0, animated: true)
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
self.progressView.setProgress(1.0, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
I've got a UIWebView on my storyboard, loading a page from my blog.
I'd like to put also a UIProgressView there... but I don't really know how to connect that to my Web View.
Can you help me?
PS: My main project is in Swift, but if you have a solution working with Objective-C, post it anyway and I'll try to convert it :)
Thanks
Thy this out in swift 3
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://topdigital.agency")
let request = NSURLRequest(url: url as! URL)
webView.loadRequest(request as URLRequest)
webView.delegate=self
}
func webViewDidStartLoad(_ webView: UIWebView) {
self.progressView.setProgress(0.1, animated: false)
}
func webViewDidFinishLoad(_ webView: UIWebView) {
self.progressView.setProgress(1.0, animated: true)
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
self.progressView.setProgress(1.0, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
One way is to use the UIWebViewDelegate. Just set the webview.delegate = self. The UIActivityIndicator will be in the right corner of the UINavigationBar. If you have no navigation bar, you can just set a position for the UIActivityIndicator and call activityIndicator.stopAnimationg() to stop the animation.
func webViewDidStartLoad(webView: UIWebView) { //start
var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .White)
var activityBarButtonItem = UIBarButtonItem(customView: activityIndicator)
navigationItem.rightBarButtonItem = activityBarButtonItem
activityIndicator.startAnimating()
}
func webViewDidFinishLoad(webView: UIWebView) { //stop
navigationItem.rightBarButtonItem = nil
}