12

Run webview_flutter example app(https://github.com/flutter/plugins/tree/master/packages/webview_flutter/example) with inital_url: "https://kreatryx.com" On Android it just shows white screen with Zopim chat bar on the bottom right. On iOS it runs fine. Any solution for android?

What I've already tried: 1. Flutter channel dev 2. Flutter channel master

enter image description here

Amarjeet Singh
  • 438
  • 1
  • 7
  • 16

8 Answers8

10

Thanks to the github Issues reference in this post, the following solution worked for me to edit Info.plist:

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

I am new to working with Info.plist, and wasn't sure if the <dict> tags could be nested like that, but apparently it's ok.

Gene Bo
  • 11,284
  • 8
  • 90
  • 137
7

Incase the url you use is not all in English or has some spacial character. You need to encode the url like this

 WebView(
      initialUrl: Uri.encodeFull(yourUrl),
      ...
 )
Boonya Kitpitak
  • 3,607
  • 1
  • 30
  • 30
5

You can try my plugin flutter_inappwebview, which is a Flutter plugin that allows you to add inline WebViews or open an in-app browser window and has a lot of events, methods, and options to control WebViews.

Full example using your URL:

import 'dart:async';

import 'package:flutter/material.dart';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: InAppWebViewPage()
    );
  }
}

class InAppWebViewPage extends StatefulWidget {
  @override
  _InAppWebViewPageState createState() => new _InAppWebViewPageState();
}

class _InAppWebViewPageState extends State<InAppWebViewPage> {
  InAppWebViewController webView;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text("InAppWebView")
        ),
        body: Container(
            child: Column(children: <Widget>[
              Expanded(
                child: Container(
                  child: InAppWebView(
                    initialUrl: "https://kreatryx.com",
                    initialHeaders: {},
                    initialOptions: InAppWebViewWidgetOptions(
                      inAppWebViewOptions: InAppWebViewOptions(
                        debuggingEnabled: true,
                      ),
                      androidInAppWebViewOptions: AndroidInAppWebViewOptions(
                        domStorageEnabled: true,
                        databaseEnabled: true,
                      ),
                    ),
                    onWebViewCreated: (InAppWebViewController controller) {
                      webView = controller;
                    },
                    onLoadStart: (InAppWebViewController controller, String url) {

                    },
                    onLoadStop: (InAppWebViewController controller, String url) {

                    },
                  ),
                ),
              ),
            ]))
    );
  }
}

It works fine in both Android and iOS platform. This is the result:

enter image description here

Lorenzo Pichilli
  • 2,896
  • 1
  • 27
  • 50
4

update

Similar to Android Webview: "Uncaught TypeError: Cannot read property 'getItem' of null"

settings.setDomStorageEnabled(true); is missing.

Related issue https://github.com/flutter/flutter/issues/26347

original

webview_flutter does currently not automatically redirect.

https://kreatryx.com redirects to https://www.kreatryx.com and the plugin doesn't follow that redirect and just shows what https://kreatryx.com returns, which is nothing.

Follow https://github.com/flutter/flutter/issues/25351

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Android uses Chrome, iOS uses Safari, so different behavior is not a big surprise even though they are supposed to behave the same as far as possible. – Günter Zöchbauer Jan 10 '19 at 10:29
  • 1
    My error is exactly the same as the error in above mentioned Android Webview issue. I just hope that settings.setDomStorageEnabled(true); fixes the issue. Anyway, thanks and I've updated my question with the screenshot on both the operating systems. – Amarjeet Singh Jan 10 '19 at 11:09
  • I was able to solve the issue using **original** answer, and posted my answer here for that. Regarding the **update** , can you please explain how to get the instance for `settings` and where the call to `settings.setDomStorageEnabled(true);` should be made .. is that in Flutter code, or in iOS code? I was not able to find any reference on that so far. – Gene Bo Aug 18 '20 at 23:27
  • 1
    @GeneBo https://github.com/flutter/plugins/pull/1836/files might help – Günter Zöchbauer Aug 19 '20 at 03:16
4

For me, I needed to enable Javascript by setting:

javascriptMode: JavascriptMode.unrestricted
WebView(
        javascriptMode: JavascriptMode.unrestricted,
        initialUrl: 'https://example.com',
)
MendelG
  • 14,885
  • 4
  • 25
  • 52
0

As there is another plugin available which has the ability to automatically redirect to its requested page. Use the given dart plugin: flutter_webview_plugin 0.3.0+2

You can use that plugin from this url: https://pub.dartlang.org/packages/flutter_webview_plugin#-readme-tab-

Sample Code found here:

new MaterialApp(
      routes: {
        "/": (_) => new WebviewScaffold(
          url: "https://kreatryx.com",
          appBar: new AppBar(
            title: new Text("Widget webview"),
          ),
        ),
      },
    );
Google
  • 2,183
  • 3
  • 27
  • 48
  • That is for the last resort. It's just that the plugin that you've mentioned works fine with a caveat that it floats over the flutter so snackbar or any such components hide behind it. – Amarjeet Singh Jan 10 '19 at 11:06
0
    Try This Guys U Can Easily Preview PDF Getting From Response.
    
    Uint8List? _documentBytes;
    @override
    void initState() {
      getPdfBytes();
      super.initState();
    }
     
    ///Get the PDF document as bytes
    void getPdfBytes() async {
 Map<String, String> _header = <String, String>{
      'Authorization': _userDataController.userDataModel.token,
      'content-type': 'application/json',
    };
      _documentBytes = await http.readBytes(Uri.parse(
          'https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf' Or 'url'));
    headers: _header,
      setState(() {});
    }
     
    @override
    Widget build(BuildContext context) {
      Widget child = const Center(child: CircularProgressIndicator());
      if (_documentBytes != null) {
        child = SfPdfViewer.memory(
          _documentBytes!,
        );
      }
      return Scaffold(
        appBar: AppBar(title: const Text('Syncfusion Flutter PDF Viewer')),
        body: child,
      );
    }
NeerO
  • 71
  • 1
  • 5
0

I have solved my issue by changing a single line in NavigationDelegate. Actually, it was causing an error in iOS White Screen.

 ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // Update loading bar.
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {},
          onWebResourceError: (WebResourceError error) {},
          onNavigationRequest: (NavigationRequest request) async {
            if (request.url.startsWith('https://www.somesite.com/')) {
              // do something...
            } else if(request.url.startsWith('http://someothersite.com/')){
              // prevent or do something else if this matches
            }
            return NavigationDecision.navigate;  // <-- Set it to navigate
          },
        ),
      ),
Taimoor Sikander
  • 605
  • 1
  • 3
  • 11