Flutter WebView:
In this tutorial, we are going to learn to create a WebView In Flutter App for both Android and IOS.
What is a WebView?
A “webview” is a browser having a URL bundled inside the mobile application and is called a hybrid app. By using this webview we are going to build mobile apps for Web technologies. but still, package it as a native app and we are putting these webview or hybrid apps into the app store. WebView makes turns your application to a web application. the beauty of webview is we no need to write browser URL in the mobile browser that displays web pages inside your application.
How WebView works in Flutter?
On iOS the WebView widget is backed by a WKWebView; On Android the WebView widget is backed by a WebView.
To use this WebView on iOS you need to opt-in for the preview of the embedded view by adding a boolean property to the app’s Info.plist
file, with the key io.flutter.embedded_views_preview
and the value will be YES
.
How the WebView Widget Works?
On iOS, the WebView widget is backed by a WKWebView, the newer and now official UIKit control for displaying web views. On Android, the WebView widget is backed by a WebView.
What is WKWEBVIEW?
An object that displays interactive web content, such as for an in-app browser
Important:
Starting in iOS 8.0 and OS X 10.10, use WKWebView to add web content to your app. Do not use UIWebView or WebView.
You can use the WKWebView class to embed web content in your app. To create a WKWebView object, set it as the view, and send it a request to load web content. You can make POST requests with httpBody content in a WKWebView.
IN the backend how it works :
After creating a new WKWebView object using the init(frame:configuration:) method, you need to load the web content. Use the loadHTMLString(_:baseURL:) method to begin loading local HTML files or the load(_:) method to begin loading web content. Use the stopLoading() method to stop loading, and the isLoading property to find out if a web view is in the process of loading. Set the delegate property to an object conforming to the WKUIDelegate protocol to track the loading of web content.
To allow the user to move back and forward through the webpage history, use the goBack()and goForward() methods as actions for buttons. Use the canGoBack and canGoForwardproperties to disable the buttons when the user can’t move in a direction. By default, a web view automatically converts telephone numbers that appear in web content to Phone links. When a Phone link is tapped, the Phone app launches and dials the number. To turn off this default behavior, set the dataDetectorTypes property with a WKDataDetectorTypes bitfield that does not contain the phoneNumber flag.
For Android:
A View that displays web pages within your app without browser URL. a WebView is a good option for displaying trusted first-party content. A WebView is useful when you need increased control over the UI and advanced configuration options that will allow you to embed web pages in a specially-designed environment for your app
Although WebView objects provide increased control over the UI, there are alternatives that may provide similar functionality with various advantages: they require less configuration, may load and perform faster, provide improved privacy protections, and can access the browser’s cookies.
Consider using these alternatives to WebView if your app falls into the following use cases:
- If you want to send users to a mobile site, build a progressive web app (PWA).
- If you want to display third-party web content, send an intent to installed web browsers.
- If you want to avoid leaving your app to open the browser, or if you want to customize the browser’s UI, useChrome Custom Tabs.
To start developing web pages for Android-powered devices using WebView objects, see the following documents.
Creating home Screen in fluter webview:
use the below line of code to create home screen in flutter webview as shown below.
void main() => runApp(MaterialApp(home: WebViewExample()));
Fetching the web page into your APP:
using the below code we are fetching web page into your app by using initial URL this initial URL method fetches this webpage into your and looks like an app and it is called a webview.
return WebView(
initialUrl: ‘https://www.iflutter.in’,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
Creating the WebView In Flutter App:
Follow the below methods to create a WebView in Flutter.
Add the dependency package:
adding the dependency package to pubspec.yaml file. use the below code to add dependency package. After adding the dependency package run the get package method to import all the required files to the app.