Search code examples
iosswiftwkwebviewsfsafariviewcontroller

Problem loading a URL in WKWebView that works fine on SFSafariViewController


I'm trying to load the following url inside a WKWebView: https://sia.unal.edu.co/ServiciosApp/facespublico/public/servicioPublico.jsf?taskflowId=task-flow-AC_CatalogoAsignaturas but it is not working as expected.

When loading it on a SFSafariViewController it works fine, but on the WKWebView it is redirected to another page.

I've managed to discover that at some point, there is a hidden redirect to something like https://sia.unal.edu.co/ServiciosApp/facespublico/public/servicioPublico.jsf;PortalJSESSION=e6xDmtoAdOmF8ust2Ar3fFL4gK9duLOgm2jEkRWIov3MUM_nT_-X!-1477800785?taskflowId=task-flow-AC_CatalogoAsignaturas.

Therefore, I started trying to load these 'new urls' into the WKWebView, but the result was the same (redirected to another page).

I also disabled redirects, and the WKWebView stays blank (and no error is sent on the delegate). I've also tried to change the cookies policy, adding javascript enabled, but the behaviour stays the same.

I would appreciate some help with this issue. As the url is public access, you can replicate the difference in behavior between the WKWebView and the SFSafariViewController.

I would also appreciate some information regarding

Why is the behaviour of this specific url so different between both options?

Solution

  • The difference is caused by the User-Agent header being different when loading via a WKWebView vs SFSafariViewController. The website you are loading must be using the User-Agent header to determine where to redirect.

    By setting the customUserAgent property on your WKWebView to the default iOS Safari User-Agent the same page will be directed to in your WKWebView as when using SFSafariViewController:

    webView.customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1"