WKWebView: [ProcessSuspension] - TimedActivity::activityTimedOut: - ios

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!

Related

WKWebView webViewDidFinishLoad only called once

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!) {
}

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

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

how to Add timeout for WKWebview

How to write a timeout handler for WKWebView, when default delegates are not getting called for didFailNavigation.
WKWebView delegate are set & DidFinishNavigation or didFailProvisionalNavigation is getting called.
Use the error.code value of the error that didFailProvisionalNavigation creates and add your 'handler' code there:
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
if error.code == -1001 { // TIMED OUT:
// CODE to handle TIMEOUT
} else if error.code == -1003 { // SERVER CANNOT BE FOUND
// CODE to handle SERVER not found
} else if error.code == -1100 { // URL NOT FOUND ON SERVER
// CODE to handle URL not found
}
}
Use this delegate method
webView:didFailProvisionalNavigation:withError:
Document
Invoked when an error occurs while starting to load data for the main frame.
And check the error code
NSURLErrorTimedOut = -1001
All the error code list
One possible solution is to add custom timer, which starts as you call loadHTML, loadRequest methods and times out on custom interval
Compared to Timer , asyncAfter(deadline:) is more light-weighted.
var isTimeOut = true
DispatchQueue.main.asyncAfter(deadline: .now() + timeOut) {
if isTimeOut{
// do time out thing
}
}
check isTimeOut according to WKNavigationDelegate
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!){
isTimeOut = false
}

Resources