Reputation: 641
I am searching for a code thru which I can refresh webView every N seconds. I got few of the brilliant answers but since I am pretty new in iOS development I am not able identify where do I need to place the refresh snippet.
Here is my code:
import UIKit
import WebKit
class ViewController: UIViewController {
var webView: WKWebView!
var websites = "www.example.com/start/home.asp"
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self as? WKNavigationDelegate
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://" + websites)!
webView.load(URLRequest(url: url) as URLRequest)
webView.allowsBackForwardNavigationGestures = true
}
//MARK:- WKNavigationDelegate
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
print(error.localizedDescription)
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Strat to load")
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
print("finish to load")
}
}
Here is the snippet which I want to add:
[NSTimer scheduledTimerWithTimeInterval:5 target:selfselector:@selector(updateWeb) userInfo:nil repeats:YES];
-(void)updateWeb
{
[webView reload];
}
Any help would be much appreciated.
Upvotes: 1
Views: 1944
Reputation: 4552
You can use NSTimer to call the method every 5 seconds and use -reload method of UIWebview.
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://" + websites)!
webView.load(URLRequest(url: url) as URLRequest)
webView.allowsBackForwardNavigationGestures = true
Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
}
@objc func update() {
webView.reload()
}
Upvotes: 2