flutter web view is not working with ios app

Viewed 2159

I have created my first flutter module which is having a single web view in it. its working fine when running the module independently or running the .ios/Runner project But when i integrate this module with my ios app and run the app then the web view is disappeared.

dart file code :

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:webview_flutter/webview_flutter.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      theme: ThemeData.light(),
      home: Home(),
    );
  }
}

// ignore: must_be_immutable
class Home extends StatelessWidget {
  String _url = "https://www.google.com";
  final _key = UniqueKey();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text("Web Viewwww"),
            leading: IconButton(
              icon: Icon(Icons.arrow_back),
              onPressed: () => SystemNavigator.pop(
                  animated: true) /*Navigator.pop(context, true)*/,
            )),
        body: Column(
          children: [
            Expanded(
                child: WebView(
                    key: _key,
                    javascriptMode: JavascriptMode.unrestricted,
                    initialUrl: _url))
          ],
        ));
  }
}

here is the code for app delegate i used:

lazy var flutterEngine = FlutterEngine()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        flutterEngine.run()
        
        return true
    }

Here is the code for iOS viewcontroller for navigation

let engin = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
 let myflutterVC = FlutterViewController(engine: engin, nibName: nil, bundle: nil)
        myflutterVC.modalPresentationStyle = .fullScreen
        present(myflutterVC, animated: true, completion: nil)

the first output is for module/runner app.
2nd output is for my ios app.
this is module/runner project output

when i run the ios app the web view disappears

2 Answers
iOS

In order for plugin to work correctly, you need to add new key to ios/Runner/Info.plist

<key>NSAppTransportSecurity</key>

<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>

If your URL has some special character then you need to encode the URL like this,

 WebView(
      initialUrl: Uri.encodeFull(yourUrl),
      ...
 )

Related