0

I am loading a URL in WKWebview in which I am passing "Accept-Language" header for displaying page as per passed language.

  var aRequest = URLRequest.init(url: MyUrl)
  aRequest.setValue("fr-CA", forHTTPHeaderField: "Accept-Language")
  self.webView.load(aRequest)

The above works properly in case of one of the URL and doesn't work in case of other. How to correctly use the header of Accept-Language?

iCodes
  • 1,382
  • 3
  • 21
  • 47

2 Answers2

0

I got inspired by How to set custom HTTP headers to requests made by a WKWebView and played around a bit with it. This is what I ended up with:

Set the navigationDelegate of the webView to self and implement

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    guard navigationAction.request.value(forHTTPHeaderField: "Accept-Language") != "fr-CA" && navigationAction.navigationType != .other else {
        decisionHandler(.allow)
        return
    }
    
    decisionHandler(.cancel)
    
    var request = navigationAction.request
    request.setValue("fr-CA", forHTTPHeaderField: "Accept-Language")
    webView.load(request)
}
Goergisn
  • 617
  • 4
  • 15
0

The locales which worked for me are as below:

  "fr-CA" - French
  "es" - Spanish
  "zh-CN" - Chinese
iCodes
  • 1,382
  • 3
  • 21
  • 47