WKWebView webViewDidFinishLoad only called once - ios

Some specific URL https://stg.express.rakuten.co.jp/?tn=600149999205 when I open the page in WKWebView it's calling didCommitNavigation multiple time (2 times) but it's calling didFinishNavigation delegate method only for one time.
Does anyone have an idea why it's happening?
I referred this post but in that case, webViewDidStartLoad/webViewDidFinishLoad called multiple time for a single request while in my case it's calling didCommitNavigation multiple times.
public func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
}
open func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
}

Related

WKWebView: [ProcessSuspension] - TimedActivity::activityTimedOut:

I'm using a WKWebView to load a page, but after might be 5 minutes I got:
[ProcessSuspension] 0x7fded682fec8 - TimedActivity::activityTimedOut:
2021-07-06 15:20:32.751176+0800 ATP Demo2[15923:9181871] [ProcessSuspension] 0x7fded68dd6c8 - TimedActivity::activityTimedOut:
2021-07-06 15:20:33.020953+0800 ATP Demo2[15923:9181871] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service
These two methods in WKNavigationDelegate are not be called.
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
fatalError("didFailProvisionalNavigation")
}
Seems there's a activityTimedOut happened. Any way to catch this error and reload the web view?
Thanks!

Blocked a frame with origin

I am trying to load javascript to a webview to change color of frame. I am getting "Blocked a frame with origin" error while javascript is being applied.
URL :
https://checkout-testing.herokuapp.com/v3/hosted/pay/7f4d4fd48adde85420e3
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let changeHtmlbuttonScript = """
document.getElementById(checkout).style.backgroundColor = "#0331FC"
"""
webView.evaluateJavaScript(changeHtmlbuttonScript) { (success, error) in
print("Error: \(error)")
}
}
I have tried setting "Arbitrary loads" property to false but no success. Does anyone know how to solve this.

WKWebView load fails randomly with NSURLErrorDomain Code -1200

I have a WKWebView created in IB that I load with an external url from Squarespace in viewWillAppear. As the page I'm loading is static I use cachePolicy .returnCacheDataElseLoad so I don't have to reload the page every time.
Usually it's working good. The page is loaded and the didFinish navigation delegate method is called. Now I started seeing some strange behavior, instead of getting the didFinish I get didFailProvisionalNavigation with SSL Error: NSURLErrorSecureConnectionFailed (-1200).
This behavior happens:
iOS 13.2.3
Only sometimes, the next time the same URL is working. Often deleting the app and reinstalling will help (removing the cache and reloading the page).
When on a mobile data connection (never when on WIFI)
Having a stable data connection (other pages in Safari are loading fine)
Only on 2 out of 8 test devices (With iPhone 11 Pro Max and iPhone X Max so far)
I don't think there are any issues with the SSL certificates because Squarespace handles that.
Any idea what can be wrong here? Otherwise what can I do to investigate more? I'm stuck!
The code:
class ViewController: UIViewController {
#IBOutlet weak var webView: WKWebView!
override func viewWillAppear(_ animated: Bool) {
if let url = URL(string: "https://www.subdomain.domain.com/article/en") {
let request = URLRequest(url: url, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 30.0)
webView.navigationDelegate = self
webView.load(request)
}
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { }
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { }
}

swift force viewController change from within webkit function

I have a viewController showing a webkit view.
If the user browses to a certain type of link, then I want to move from that viewController to a new one, which can parse that specific type of url and display results (rather than moving to a new page)
However, as I understand it, in order to check the url links to see it's of the right kind then I need to use the function
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
but this only gives me the ability to write a bit of code to look at the URL link, and return a decision on whether to proceed to it or not, rather than what I want to do which is look at the URL link and then effectively segue to a completely new viewController to parse it.
Any ideas on how I should construct please?
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Swift.Void)
{
if(navigationAction.navigationType == .other)
{
if navigationAction.request.url != nil
{
// Put your condition check on url.
// Create your view controller instance from storyboard. Pass the url to it and navigate.
}
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}

Can WKWebView instance show webpage while it is loading?

I'm using WKWebView to browse the specific website.
WKWebView instance loads initial page of the website by URL with method load(_ request: URLRequest) -> WKNavigation? Until load request isn't completed I see white screen. Can WKWebView show already loaded parts of the webpage while the rest of the webpage is loading?
Can UIWebView do this trick?
if white screen is your issue, i recommend you to display an custom progress indicator over the webView or some Animation view over your webView.
Once the loading is completed hide the progress indicator.
Start your Animation at:
optional func webView(_ webView: WKWebView,
didStartProvisionalNavigation navigation: WKNavigation!)
{
//Start Progress indicator animation
}
End/Hide your Animation/Progress View at:
optional func webView(_ webView: WKWebView,
didFinish navigation: WKNavigation!)
{
//Stop Progress indicator animation
}
In my case progressive loading started to work when i have added delegate WKWebViewNavigationDelegate to WKWebview and function
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!)
If this func was not added, Web page did not rendered before all content was downloaded in all cases

Resources