I have 2 views on my screen first is the Webview and the second is a static view "errorView" with the error message "set to hidden". I am trying to load the errorView if we have an error loading website to Webview.
#objc private func loadURL() {
guard let url = URL(string: "https://www.hackingwi") else {
self.errroLoadingWebView.isHidden = false
return
}
webView?.load(URLRequest(url: url))
webView?.allowsBackForwardNavigationGestures = true
}
You can use WKNavigationDelegate.
set delegate
webView.uiDelegate = self
webView.navigationDelegate = self
class StaticWebPageVC: WKNavigationDelegate, WKUIDelegate {
func webView(_: WKWebView, didFinish _: WKNavigation!) {
// Web view loaded
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
// Show error view
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
// Show error view
}
}
Related
I am working on an app that mainly consist of a WKWebView.
I monitor it's navigation status using the WKNavigationDelegate protocol.
I create my viewController as a WKNavigationDelegate
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate, UIGestureRecognizerDelegate {
...
override func viewDidLoad() {
...
webView.navigationDelegate = self
...
I load the request on the webview like so:
let gotoUrl = ... as? String
if let url = URL(string: gotoUrl) {
let request = URLRequest(url: url)
webView.load(request)
}
And then the delegate methods:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("Successfully loaded")
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
print("Did commit navigation")
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Start Provisional navigation")
}
func webViewWebContentProcessDidTerminate(_ webView:WKWebView) {
print("request failed")
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("request failed")
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation: WKNavigation!, withError: Error) {
print("request failed")
}
This works fine for my webapps initial url: https://example.com/app/
Then the delegate methods are called.
When the app is opened from a notification I want to direct the webview to a specific part of the webapp.
However when i load an url that load a specific part of the webapp:
https://example.com/app/#/secure/content/alarm-base/alarm-list//alarm-detail/1381122
The delegate methods are not called, although the webview loads the url successfully.
The delegate methods are also called if I load the specific url directly on startup.
The web appication is written in AngularJS.
I would like for the delegate handlers to trigger for every request I load from the code.
Any idea on how to achieve this?
I am not sure what is going on, but it is possible to force it to use the delegate by adding the following code when doing the request:
webView.load(request)
webView.stopLoading()
webView.reload()
It is clunky, but it solves the problem for me.
I'm getting a URL from API, I'm passing that URL to my webview and trying to load that, but it isn't loading. I don't know why but it isn't working. Even it's not showing any error or crashes. It's so weird but I don't know what I'm missing in that all the code is written fine. This is my code:
self.loadWeb(webURL: newsUrl!)
func loadWeb(webURL: String) {
let url = URL(string: webURL)
MBProgressHUD.hide(for: self.view, animated: true)
webView.navigationDelegate = self
webView.uiDelegate = self
webView.load(URLRequest(url: url!))
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.frame.size.height = 1
webView.scrollView.isScrollEnabled = false
webView.frame.size = webView.scrollView.contentSize
self.bgViewHeight.constant = webView.frame.size.height
}
private func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
print(error.localizedDescription)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Start to load")
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
print("finish to load")
}
How i can load this?
i want to hide certain html elements in wkwebview using swift 4, xcode 9. i know how to do it in android and have added the code below. i just need an equivalent code for ios in swift 4 for wkwebView.
myWebView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(WebView view, String url)
{
myWebView.loadUrl("javascript:(function() { " +
"document.getElementById('divHeader')
[0].style.display='none'; " +
"})()");
}
});
myWebView.loadUrl(URL);
Use WKWebView
import WebKit
class FirstViewController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var webVw: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webVw.navigationDelegate = self
loadmyPage(htmlstr: "https://www.mywebsite.com")
}
func loadmyPage(htmlstr:String){
let url = URL (string: htmlstr)
let request = URLRequest(url: url!)
webVw.load(request)
}
//MARK:- WKNavigationDelegate
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print(error.localizedDescription)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Start to load")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("finish to load")
let elementID = "right_sidebar"
let removeElementIdScript = "var element = document.getElementById('\(elementID)'); element.parentElement.removeChild(element);"
webView.evaluateJavaScript(removeElementIdScript) { (response, error) in
debugPrint("Am here")
}
}
I'm trying to add a WKWebView inside of a UIView ,
The code seems to work fine ( the page is loaded the bounds is good the delegate print the message with no error)
but the View (called ViewForStuffInWeb ) stay blank, nothing in it
can someone explain why ?
import UIKit
import WebKit
class ViewController: UIViewController ,WKNavigationDelegate {
#IBOutlet var ViewForStuffInWeb: UIView!
var webView = WKWebView()
let request = "https://www.google.fr"
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.automaticallyAdjustsScrollViewInsets = false
let config = WKWebViewConfiguration()
webView = WKWebView(frame: ViewForStuffInWeb.bounds ,configuration : config)
webView.navigationDelegate = self
ViewForStuffInWeb = webView
println("webview : frame :\(webView.frame) , bounds : \(webView.bounds)")
requesting(request)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func requesting (request :String) {
if let url = NSURL(string: request) {
webView.loadRequest(NSURLRequest(URL: url))
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
println("decide policy action")
decisionHandler(WKNavigationActionPolicy.Allow)
}
func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void) {
println("decide policy response")
decisionHandler(WKNavigationResponsePolicy.Allow)
}
func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation!) {
println("commit navigation")
}
func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) {
println("fail in didFailNavigation \(error)")
}
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
println("fail in didFailProvisionalNavigation \(error)")
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
println("finish navigation")
}
thanks for reading
You never add the WKWebView to the view hierarchy. In your viewDidAppear, which code should really move to viewDidLoad, you probably want to replace:
ViewForStuffInWeb = webView
with:
ViewForStuffInWeb.addSubview(webView)
Although it is unclear what ViewForStuffInWeb actually is. The above will only work if that view exists in your nib and if it is connected.
I am very new to Swift and iOS development so thank you so much in advance for helping me!
I have tried every example online and every page on this site and I cannot get my Activity Indicator to stop being displayed once the page completes loading in my WKWebview.
Any assistance would be so appreciated! Thank you!
import UIKit
import WebKit
class FirstViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
#IBOutlet var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self
activityIndicator.startAnimating()
activityIndicator.isHidden = true
activityIndicator.hidesWhenStopped = true
let url = Bundle.main.url(forResource: "Web/bulk_material_table", withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
self.webView.load(request)
}
func showActivityIndicator(show: Bool) {
if show {
activityIndicator.startAnimating()
} else {
activityIndicator.stopAnimating()
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
showActivityIndicator(show: false)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
showActivityIndicator(show: true)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
showActivityIndicator(show: false)
}
}
Just replace
webView.uiDelegate = self
with
webView.navigationDelegate = self
And it will work because
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
showActivityIndicator(show: false)
}
is WKNavigationDelegate's delegate method.