Page 1 of 1

WebView processing completion method

Posted: Sat Jan 18, 2025 7:06 am
by ishanijerin1
Timeout handling
It stops the WebView from loading and executes the completion method (completeAndCleanUp).

private func handleTimeout () {
webView.stopLoading()
completeAndCleanUp()
}
7. WebView processing completion slovakia email address method (normal)
Called when the WebView has finished loading successfully.

It also disables the timer and performs completion processing.

func webView (_ webView : WKWebView , didFinish navigation : WKNavigation! ) {
timer?.invalidate()
completeAndCleanUp()
}
8. (abnormal)
Called when the WebView fails to load.

As with normal cases, disable the timer and execute the completion process. If you want to execute a different process only for abnormal cases, write it in the method.

// Error occurred during loading
func webView (_ webView : WKWebView , didFail navigation : WKNavigation! , withError error : Error ) {
timer?.invalidate()
// Just close the WebView
completeAndCleanUp()
}
// Error when starting to load (out of range, etc.)
func webView (_ webView : WKWebView , didFailProvisionalNavigation navigation : WKNavigation! , withError error : Error ) {
timer?.invalidate()
// Close the WebView even if an exception error occurs.
completeAndCleanUp()
}
9. Completion Method
completionInvokes the Closure to signal completion of the request.
Remove the WebView from its parent view to prevent memory leaks.
private func completeAndCleanUp () {
completion?()
webView.removeFromSuperview()
}
10. URL Setting Methods
Specify the URL required to load the WebView and return it with URLRequest. The URL and header are set separately for logged in and logged out cases. In my implementation example, the purpose was to send login information, so the login information (token) was set in the "login token" section. If you have a different use case, you can change the header as needed.