flutter webview disallowed user agent - flutter

I am trying to openning sign-in google with webview in flutter application. I installed flutter_inappwebview: ^3.1.0 and this is my code:
child: InAppWebView(
initialUrl: url.toString(),
initialHeaders: {},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
userAgent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
debuggingEnabled: true,
)),
but when i open google sin in page i got this error:

Using webview_flutter package you can fix the problem by adding userAgent: 'random', in the web view constructor. So, it will look like this:
WebView(
initialUrl: 'https://abdulrazakzakieh.com/',
userAgent: 'random',
)

change userAgent to random like -> userAgent: "random". works perfectly! but then it will most likely low end mobile interface for google page. that's the only drawback. else works perfectly fine !

For my case, It worked for this-
InAppWebView(
initialUrlRequest: URLRequest(url: Uri.parse("www.myurl.com")),
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(userAgent: "random"),)
...)

Related

flutter inappwebview : "window.flutter_inappwebview.callHandler" dosn't work in ios

javascript call “ window.flutter_inappwebview.callHandler('handlerFoo') ”
version: ^5.7.2+2
android is ok,but can't use in ios;
In addition, it is found that consoleMessage is invalid under ios.
my options:
options = InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
useShouldInterceptFetchRequest: true,
),
android: AndroidInAppWebViewOptions(
useHybridComposition: true,
),
ios: IOSInAppWebViewOptions(
allowsInlineMediaPlayback: true,
),
);
enter image description here
enter image description here
by the way ,There is a very old project in which he uses webview interaction within ios:
"window.webkit.messageHandlers.openScan.postMessage({})"
How to intercept the request "window.webkit.messageHandlers.openScan.postMessage ({})" without modifying the original web page project?
Thank you very much for your help
Can you run it for me?

How to send parameters to assets file webview in flutter?

I want to open my index.html in the assets folder but with parameters, I tried to open like this:
initialFile: "assets/index.html?name=foo&lastname=Bar" but it doesn´t work.
return Container(
child: InAppWebView(
//initialFile: "assets/index.html",
initialOptions: inAppWebViewGroupOptions,
initialUrlRequest: URLRequest(
url: Uri.parse("file:///assets/index.html"),
method: 'GET',
body:
Uint8List.fromList(utf8.encode("name=foo&lastname=Bar")),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}),
),
);
}
maybe you can just set WebView cookie to pass paramerters.
You can use In-App Localhost Server.

Flutter webivew not showing content from my site

I am trying to load my website content in my flutter application.
I have a link https://dev.demo.hello.example.com/id/4
I have tried using flutter_webview_plugin & webview_flutter both plugins but on both plugins i am getting below error :
WF: === Starting WebFilter logging for process Runner
WF: _userSettingsForUser mobile: {
filterBlacklist = (
);
filterWhitelist = (
);
restrictWeb = 1;
useContentFilter = 0;
useContentFilterOverrides = 0;
whitelistEnabled = 0;
}
WF: _WebFilterIsActive returning: NO
I have also tried mentioned ATS related things in info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
But still facing the same issue. I have https in my link.
Is there any work around ?
In my case, I was setting a navigationDelegate to prevent all further navigation :
WebView(
initialUrl: url,
javascriptMode: JavascriptMode.unrestricted,
navigationDelegate: (navigation) => navigation.url == url ? NavigationDecision.navigate : NavigationDecision.prevent,
),
It works fine on Android, the initialUrl gets loaded, and any further navigation is blocked.
But on iOS I had to change this code as it was blocking even initialUrl :
WebView(
initialUrl: url,
javascriptMode: JavascriptMode.unrestricted,
navigationDelegate: (navigation) => navigation.url == url ? NavigationDecision.navigate : NavigationDecision.prevent,
),
Yes, just put this code to your navigationDelegate in WebView widget
navigationDelegate: (NavigationRequest request) {
if (request.url == get.url) {
return NavigationDecision.navigate;
}
return NavigationDecision.prevent;
}
it work for me on IoS & Android

How to save sessionId of first login and doesn't make me login every time in inappwebview flutter

I want to write an application that makes me register from app and send post request and get response to inappwebview just like exam below
InAppWebView(
initialUrl: widget.initUrls,
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
mediaPlaybackRequiresUserGesture: false,
debuggingEnabled: true,
),
),
onWebViewCreated: (InAppWebViewController c){
_controller = c;
if(staticEmail != null && staticPass != null) {
_controller.postUrl(url: "https://xxxxxxx.com/my-account", postData: utf8.encode(
"email=${staticEmail}&password=${staticPass}&wooc_user_phone=${staticShop}&wooc_user_name=${staticShop}&woocommerce-register-nonce=5d1b626841&_wp_http_referer=/my-account/&register=Register") )
.whenComplete(() => {print("done")}).catchError((err){print("err : ${err}");
isloading = false;
});
}
},
);
and it login fine.
But when I go to another link or when I close app and login again it ask me to login again i just want to save cookie or session id that doesn't make me login every time
You can make use of the CookieManager of this flutter_inappwebview package to get and set cookies. This is a singleton that the plugin uses, you can access it via:
CookieManager.instance()

Problem in google login in canva through webview in flutter

I wanted to upload pics through canva from my flutter app that is why I am using the flutter-webview-plugin for the first time and therefore I am not able to solve this issue.
Code for the same is:-
import 'package:flutter/material.dart';
import 'package:kf_drawer/kf_drawer.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'dart:async';
class SettingsPage extends KFDrawerContent {
#override
_SettingsPageState createState() => _SettingsPageState();
}
class _SettingsPageState extends State<SettingsPage> {
final Completer<WebViewController> _controller =
Completer<WebViewController>();
#override
Widget build(BuildContext context) {
return SafeArea(
child: Center(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
....
],
),
Expanded(
child: Container(
height: 500,
child: WebView(
initialUrl: "https://shree-hari.github.io/laxmi_canva/index.html",
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController){
_controller.complete(webViewController);
},
),
),
)
],
),
),
);
}
}
Please Help me to address this issue 🙄.
Google not allow native Flutter Web-Views to initiate OAuth.
For more info read Google Blog
In your case, I can suggest 3 Possible Solutions.
Try to Sign in with Email/Password instead of Google Sign In.
Use url_launcher to redirect the user to the browser.
If you don't want the user to leave your app
then you can use flutter_custom_tabs
this plugin use Chrome Custom Tabs to create a native experience inside the Flutter App.
You can add this userAgent :
WebView(
initialUrl: url,
userAgent: Platform.isIOS ? 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_1_2 like Mac OS X) AppleWebKit/605.1.15' +
' (KHTML, like Gecko) Version/13.0.1 Mobile/15E148 Safari/604.1' :
'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) ' +
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Mobile Safari/537.36',
...
Use this package, it works like a charm
https://github.com/LinusU/flutter_web_auth
In the background, this plugin uses ASWebAuthenticationSession on iOS 12+ and macOS 10.15+, SFAuthenticationSession on iOS 11, Chrome Custom Tabs on Android and opens a new window on Web.
Your auth flow must land in the scheme you provide in callbackUrlScheme property. Then just parse the callback url and implement you own business logic around signin/signup
import 'package:flutter_web_auth/flutter_web_auth.dart';
// Present the dialog to the user
final result = await FlutterWebAuth.authenticate(url: "https://my-custom-app.com/connect", callbackUrlScheme: "my-custom-app");
// Extract token from resulting url
final token = Uri.parse(result).queryParameters['token']
https://stackoverflow.com/a/71297966/18723243
Using webview_flutter package you can fix the problem by adding userAgent: 'random', in the web view constructor. So, it will look like this:
WebView(
userAgent: 'random',
)