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
Related
so I can't seem to figure out a way to perform some tasks before loading a url in ios webview.
Like in android we can use shouldOverrideUrlLoading.
#Override
public boolean shouldOverrideUrlLoading(final WebView view, String url) {
///sometask
}
but I can't find a function that does something like this in ios.
Say a user clicked on a href tag inside the webview that takes it to different page, how do I know what link that tag takes the user to? before changing the page.
Basically I want to check what url the webpage is about to load and perform extra steps according to that url.
You can use WKNavigationDelegate
set navigationDelegate in viewDidLoad
self.webView?.navigationDelegate = self
Observe URL in below navigationDelegate Method.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
DispatchQueue.main.async {
if let urlString = navigationAction.request.url?.absoluteString{
print(urlString)
}
}
decisionHandler(.allow)
}
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!) {
}
I'm trying to load a local web, but is not working, only a white screen is being displayed.
let zipName = webSection?.zipResource?.zipName?.lowercased() {
let zipPath = ProjectPath.path.appending(zipName.deletingPathExtension).appending(pathComponent: "index.html")
let url = URL(fileURLWithPath: zipPath)
//webView.loadFileURL(url, allowingReadAccessTo: url) //suppossedly new method for loading local websites, but it haves same behaviour, blank screen
webView.load(URLRequest(url: url))
In Android, I can see in logcat which errors is generating the webview when no content is being displayed, but here in Xcode I can't see nothing in the console. How can i see which errors is giving the WKWebView?
Implement webView navigation delegate in viewDidLoad
self.webView.navigationDelegate = self
extension ViewController : WKNavigationDelegate {
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print(error.localizedDescription)
}
}
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) { }
}
There is a social link button inside WKWebView. When I click on the button nothing happens.
Could you please tell me how to open the link by clicking on the button?
Thanks in advance.
what link u want to open change "facebook","twitter" and you can add more links also
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
if let url = navigationAction.request.url ,UIApplication.shared.canOpenURL(url) {
// let urlString = try! String(contentsOf: url)
if (url.absoluteString.range(of: "facebook.com") != nil || url.absoluteString.range(of: "twitter.com") != nil){
//UIApplication.shared.open(url)
print("Redirected to browser. No need to open it locally")
decisionHandler(.cancel)
}else{
print("Open it locally")
decisionHandler(.allow)
}
}
You need a on click listener.
You can use WKWebView to display interactive web content in your app. Ideal for displaying HTML markup, styled text content, or complete web pages. After that, you need to handle the Social button you're calling and the data submitted if any...
Responding To WKWebView Events With WKNavigationDelegate
A WKWebView web view has two main delegate protocols and properties:
navigationDelegate of type WKNavigationDelegate, which responds to
navigation events
uiDelegate of type WKUIDelegate, which responds to user
interaction events
Of these two delegate protocols, the WKNavigationDelegate is probably used most frequently. So, let’s hook into some page navigation events! We’ll do so with these delegate functions:
webView(_:didStartProvisionalNavigation:)
webView(_:didCommit:)
webView(_:didFinish:)
webView(_:didFail:withError:)
webView(_:didFailProvisionalNavigation:withError:)
First, let’s adopt the protocol and set the webView delegate. Here’s how:
Add the WKNavigationDelegate protocol to your view controller’s class declaration, like: class WebViewController: UIViewController, WKNavigationDelegate
Set the navigationDelegate property of webView to self, before loading the web request, like: webView?.navigationDelegate = self
Next, implement the five delegate functions described earlier. Set their function bodies to print(#function), so we can see the order in which the functions are called.
Like this:
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)
{
print(#function)
}