Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[webview_flutter] NavigationDecision.prevent not working on Android #68785

Open
intenseandnoble opened this issue Oct 22, 2020 · 7 comments
Open
Labels
found in release: 2.2 Found to occur in 2.2 found in release: 2.5 Found to occur in 2.5 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: webview The WebView plugin P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. platform-android Android applications specifically team-android Owned by Android platform team triaged-android Triaged by Android platform team

Comments

@intenseandnoble
Copy link

Hi, this is my setup

Flutter 1.22.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 84f3d28555 (7 days ago) • 2020-10-15 16:26:19 -0700
Engine • revision b8752bbfff
Tools • Dart 2.10.2
...
webview_flutter: ^1.0.5

As the title says, I am trying to prevent navigation. In my current case, I want to prevent all navigation. It is to test another issue.

This following sample does not prevent navigation.

WebViewController _controller;
...
WebView(
        initialUrl: main.URL,
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController controller) {
          _controller = controller;
        },
        javascriptChannels: <JavascriptChannel>[
          _loginJavascriptChannel(context),
        ].toSet(),
        onPageFinished: (url) {
          _controller.evaluateJavascript(javascriptString);
        },
        navigationDelegate: (NavigationRequest request) {
          return NavigationDecision.prevent;
        },
      ),
@TahaTesser
Copy link
Member

I can see it's not working on Android and on iOS, the initial page isn't loaded #65502

complete code sample
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Completer<WebViewController> _controller = Completer<WebViewController>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(color: Colors.white),
        title: Text(
          'Webview test',
          style: TextStyle(
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
        backgroundColor: Color.fromRGBO(56, 64, 82, 1),
      ),
      body: WebView(
        initialUrl: 'https://flutter.dev',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController controller) {
          _controller.complete(controller);
        },
        navigationDelegate: (NavigationRequest _redirectRequest) {
          return NavigationDecision
              .prevent; //Blocks all internal html navigation requests.
        },
      ),
    );
  }
}
flutter doctor -v
[✓] Flutter (Channel stable, 1.22.2, on Mac OS X 10.15.7 19H2, locale en-GB)
    • Flutter version 1.22.2 at /Users/tahatesser/Code/flutter_stable
    • Framework revision 84f3d28555 (7 days ago), 2020-10-15 16:26:19 -0700
    • Engine revision b8752bbfff
    • Dart version 2.10.2

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/tahatesser/Code/sdk
    • Platform android-30, build-tools 30.0.2
    • ANDROID_HOME = /Users/tahatesser/Code/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.1, Build version 12A7403
    • CocoaPods version 1.10.0.rc.1

[!] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.50.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.15.1

[✓] Connected device (1 available)
    • RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 10 (API 29)

! Doctor found issues in 1 category.

There is a deep investigation on this issue in #65502 (comment)

@TahaTesser TahaTesser added found in release: 1.22 Found to occur in 1.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: first party p: webview The WebView plugin platform-android Android applications specifically labels Oct 22, 2020
@TahaTesser TahaTesser changed the title webview_flutter NavigationDecision.prevent not working [webview_flutter] NavigationDecision.prevent not working on Android Oct 22, 2020
@cyanglaz
Copy link
Contributor

@intenseandnoble There is a behavior difference between android and iOS. on Android, the first url (in your case, the main.URL) is always loaded.
We are leaning towards making iOS behaves similar to android. (Always loading the first url)

Is there any reason that you explicitly ask the webview to load a url and then you want to prevent it from loading?

@intenseandnoble
Copy link
Author

intenseandnoble commented Oct 22, 2020

The loading of the initial url is not the issue why I am here. I gave a simplified example.

The main issue is the one I wanted to test (which I refered to briefly in my first comment). It is that I want to evaluate a string of Javascript at each navigation.

Unable to do so, I tried preventing navigation and creating a new webview with said javascript, after the initial url has been loaded. It did not work. Uncertain whether it was my code or a bug, I tested, found this bug and created a StackOverflow question about my actual issue.

@aseef17
Copy link

aseef17 commented Feb 28, 2021

Are there any updates about this?
I'm not sure if this is considered a priority issue but if it's not, I assume that this should be.

@intenseandnoble
Copy link
Author

intenseandnoble commented Feb 28, 2021

I got it to work in a roundabout way. Through trial and error, I came to conclude that evaluateJavascript is evaluated once but always kept.

So my solution was to put the js code in an interval. Due to the nature of the application, it seems a satisfying solution. (The application is mainly used to find documents, so it is a "search, download file and close app" situation)

*thanks for asking about an update, it gives me an opportunity to give back to the community

@danagbemava-nc
Copy link
Member

Reproducible with master 2.5.0-6.0.pre.63 and stable 2.2.3 with webview_flutter: ^2.0.12.
The initial url is always loaded, however any other links are blocked from opening.

flutter doctor -v

[✓] Flutter (Channel master, 2.5.0-6.0.pre.63, on macOS 11.5.1 20G80 darwin-arm,
    locale en-GH)
    • Flutter version 2.5.0-6.0.pre.63 at /Users/nexus/dev/sdks/flutters
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 047d35d8ba (2 hours ago), 2021-08-12 12:47:05 +0800
    • Engine revision a2e60472a9
    • Dart version 2.15.0 (build 2.15.0-5.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/nexus/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.5.1, Build version 12E507
    • CocoaPods version 1.10.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] VS Code (version 1.59.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.25.0

[✓] Connected device (4 available)
    • M2007J20CG (mobile)         • 5dd3be00                  • android-arm64  •
      Android 11 (API 30)
    • sdk gphone64 arm64 (mobile) • emulator-5554             • android-arm64  •
      Android 12 (API 31) (emulator)
    • Nexus’ IPhone (mobile)      • 00008020-001875E83A38002E • ios            •
      iOS 14.6 18F72
    • Chrome (web)                • chrome                    • web-javascript •
      Google Chrome 92.0.4515.131

• No issues found!
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.5.1 20G80 darwin-arm, locale
    en-GH)
    • Flutter version 2.2.3 at /Users/nexus/dev/sdks/flutter
    • Framework revision f4abaa0735 (6 weeks ago), 2021-07-01 12:46:11 -0700
    • Engine revision 241c87ad80
    • Dart version 2.13.4

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/nexus/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.5.1, Build version 12E507
    • CocoaPods version 1.10.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] VS Code (version 1.59.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.25.0

[✓] Connected device (4 available)
    • M2007J20CG (mobile)         • 5dd3be00                  • android-arm64  •
      Android 11 (API 30)
    • sdk gphone64 arm64 (mobile) • emulator-5554             • android-arm64  •
      Android 12 (API 31) (emulator)
    • Nexus’ IPhone (mobile)      • 00008020-001875E83A38002E • ios            •
      iOS 14.6
    • Chrome (web)                • chrome                    • web-javascript •
      Google Chrome 92.0.4515.131

• No issues found!

@danagbemava-nc danagbemava-nc added found in release: 2.2 Found to occur in 2.2 found in release: 2.5 Found to occur in 2.5 and removed found in release: 1.22 Found to occur in 1.22 labels Aug 12, 2021
@stuartmorgan stuartmorgan added the P3 Issues that are less important to the Flutter project label Aug 16, 2021
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
@Hixie Hixie removed the plugin label Jul 6, 2023
@flutter-triage-bot flutter-triage-bot bot added multiteam-retriage-candidate team-android Owned by Android platform team triaged-android Triaged by Android platform team labels Jul 8, 2023
@whiteLT
Copy link

whiteLT commented Aug 23, 2023

You can try to load some dummy URL inside prevent, that should stop the url from changing. Temp workaround

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
found in release: 2.2 Found to occur in 2.2 found in release: 2.5 Found to occur in 2.5 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: webview The WebView plugin P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. platform-android Android applications specifically team-android Owned by Android platform team triaged-android Triaged by Android platform team
Projects
None yet
Development

No branches or pull requests

8 participants