Search code examples
iosflutterwebviewwkwebviewwebview-flutter

Flutter: webview_flutter just displays blank (white) on iOS


I got it to work on Android, the WebViewWidget displays correctly with the html content, no problem.

For iOS it just displays blank/white. I tried what I have seen suggested by people all over the internet, which is to add a few lines to Info.plist, that allow the WKWebView in iOS to load and display http (not secure, not only https):

<key>io.flutter.embedded_views_preview</key>
<string>YES</string>
<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
  <key>NSAllowsArbitraryLoadsInWebContent</key>
  <true/>
</dict>

(^^I don't really know what these lines individually do)
(^^I have also tried lowercase "yes" and <true/> as values for the first one)

I ran flutter clean, then flutter pub get, deleted Podfile.lock, ran pod install (in the ios folder) a bunch of times, but I still can't get the content to show in the webview on iOS (it works on Android), only blank white space where the html content is supposed to appear.

It's important to mention that I get my html from neither http nor https, but I load a String (which is html) and set it like this:

String text = ...
await _controller.loadHtmlString(text);

Is there maybe a way of tricking iOS to think that the html comes from https?


These are some common messages I see printed out in the console (not red, just regular color), and I only get these messages when running the app from android studio, not XCode (on a real iOS device):

[assertion] Error acquiring assertion: <Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}>
[ProcessSuspension] 0x12d07df20 - ProcessAssertion::acquireSync Failed to acquire RBS assertion 'ConnectionTerminationWatchdog' for process with PID=47861, error: Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}

and:

Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service

UPDATE: I found out, it also shows a blank/white page on iOS even when I use https, like _controller.loadRequest(Uri.parse('https://flutter.dev'));.


Solution

  • I solved the problem, it was unexpected and not related to http/https stuff... See the comments in below code for explanation

        _controller.setNavigationDelegate(
        //...
          onNavigationRequest: (NavigationRequest request) {
            print('onNavigationRequest');
            //I first had this line to prevent redirection to anywhere on the internet via hrefs
            //but this prevented ANYTHING from being displayed
            // return NavigationDecision.prevent;
    
            return NavigationDecision.navigate; //changed it to this, and it works now
          },
        );
    

    I also removed all those lines from Info.plist, and it still works.