Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 12 wkwebview not working with redirects?

I have a basic webview that loads a website that is fronted by an nginx reverse proxy that is just forwarding it to another site. I am able to load it using safari, chrome firefox etc on the device and emulator (as well as computer), but when I try to load it in the wkwebview it flashes a couple times then goes to a blank white screen. Note this same app worked fine in iOS 10 - 11, but is now broke with iOS 12. Below is a simple code excerpt that shows what I'm doing:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {

var webView: WKWebView!

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    view = webView
}

override func viewDidLoad() {
    super.viewDidLoad()

    let myURL = URL(string:"https://test.com")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

I've attempted adding the following to my Info.plist, which also did not work:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>test.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubDomains</key>
            <true/>
        </dict>

It also shows this in the logs in xcode:

[BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2] . 
[0x7f82f8d0efc0] get output frames failed, state 8196

When I try to debug it using Safari Dev Tools it shows that it's trying to load about:blank, which is strange, because again - it works in all other browsers. On the nginx side all I'm doing is a simple proxy_pass rule and when I hit it the endpoint in the app I can see in the nginx access logs that it responds with a 200. Anyone have ANY ideas?

like image 618
Mitch Davis Avatar asked Oct 07 '18 03:10

Mitch Davis


People also ask

Is WKWebView the same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

What is the difference between UIWebView and WKWebView?

Unlike UIWebView, which does not support server authentication challenges, WKWebView does. In practical terms, this means that when using WKWebView, you can enter site credentials for password-protected websites.

How do I refresh WKWebView?

The WKWebView already contains a scrollview. All you need to do is create the refresh control, assign a target function that will get called when a user initiates a refresh, and attach the refresh control to the scrollview.

What is IOS WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


1 Answers

I had the same problem and I solved it this way through the WKNavigationDelegate:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if navigationAction.navigationType == .linkActivated {
        guard let url = navigationAction.request.url else {return}
        webView.load(URLRequest(url: url))
    }
    decisionHandler(.allow)
}

Hope it helps

like image 137
Pablo Blanco Avatar answered Oct 26 '22 23:10

Pablo Blanco