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] onPageFinished never gets called on iOS with an invalid URL #74987

Open
S4b3 opened this issue Jan 29, 2021 · 14 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 P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. platform-ios iOS applications specifically team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team

Comments

@S4b3
Copy link

S4b3 commented Jan 29, 2021

I'm working with webview_flutter in order to Auth my users through Cognito's auth endpoint.

This is my WebView:

    return WebView(
        initialUrl: url,
        userAgent: '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',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) {
          _webViewController.complete(webViewController);
        },
        navigationDelegate: (NavigationRequest request) async {
          print("Current url : ${request.url}");
          if (request.url.startsWith('myapp://?code=')) {
            print("Preventing navigation");
            return NavigationDecision.prevent;
          }
          print("Navigated to url");
        },
        onPageFinished: (String url) async {
          print("onPageFinished with url: $url");
          if (url.startsWith('myapp://?code=')) {
            code = url.substring('myapp://?code='.length).split('#')[0];
            final Map<String, String> userData = await globals.cognitoClient
                .signUserInWithThirdPartyAuthCode(code);
            final UserModel user = await globals.appSyncClient.fetchMyProfile();
            userState.user = user;
            if (user.username != null && user.username != '') {
              Navigator.pushNamedAndRemoveUntil(
                  context, '/home', (Route<dynamic> route) => false);
            } else {
              Navigator.pushReplacement(context, MaterialPageRoute<dynamic>(
                builder: (BuildContext context) {
                  return TpSignUpPage(
                    name: userData['name'],
                    surname: userData['surname'],
                    tptype: widget.tpType,
                  );
                },
              ));
            }
          }
        },
        gestureNavigationEnabled: true,
      );

Preventing navigation when the url contains my auth code allows me to do the magic inside onPageFinished and on Android everything works fine.

On the other hand, on iOS the onPageFinished callback never gets called. The print never shows up in the console and the navigation stops at the auth code without nothing getting done. I've even checked if requests are for the main frame, they are.
This is what i get:

flutter: Current url : https://xxxxxxxxxxx.auth.eu-central-1.amazoncognito.com/oauth2/authorize?identity_provider=Facebook&redirect_uri=myapp://&response_type=CODE&client_id=xxxxxxxxxxxxxxxxxxxxxx&scope=email%20openid%20profile%20aws.cognito.signin.user.admin
flutter: Navigated to url
flutter: Current url : myapp://?code=4xxxxxxx-xxx8-xxxx-a88a-7xxxxxxxxx3
flutter: Preventing navigation

Whereas after every navigation onPageFinished's supposed to be called.
I really don't know how or why this might happen; I've searched through many issues and my current Info.plist contains :

    <key>io.flutter.embedded_views_preview</key>
    <true/>

and this is my flutter doctor -v:

[✓] Flutter (Channel stable, 1.22.5, on Mac OS X 10.15.7 19H2 darwin-x64, locale it-IT)
    • Flutter version 1.22.5 at /usr/local/Caskroom/flutter/1.22.0/flutter
    • Framework revision 7891006299 (7 weeks ago), 2020-12-10 11:54:40 -0800
    • Engine revision ae90085a84
    • Dart version 2.10.4

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/sabe/Library/Android/sdk
    • Platform android-30, build-tools 30.0.2
    • 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.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.2, Build version 12B45b
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 50.0.1
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[!] IntelliJ IDEA Ultimate Edition (version 2019.1.3)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.dev/intellij-setup/#installing-the-plugins

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

[✓] Connected device (1 available)
    • iPhone 12 Pro Max (mobile) • 83C45AD2-68CE-4260-B932-57C4B5B861D4 • ios •
      com.apple.CoreSimulator.SimRuntime.iOS-14-2 (simulator)

! Doctor found issues in 1 category.

(I don't use IntelliJ anymore so w/ever)

Steps to Reproduce : I'm assuming just run a webview on ios having onPageFinished populated ?
What could be causing this issue? Pls send help

@TahaTesser
Copy link
Member

Hi @S4b3
Can you please provide` a complete reproducible minimal code sample
I tried to reproduce using the official example

flutter: Page finished loading: https://flutter.dev/
flutter: allowing navigation to NavigationRequest(url: https://flutter.dev/docs/testing/debugging, isForMainFrame: true)
flutter: Page started loading: https://flutter.dev/docs/testing/debugging
flutter: Page finished loading: https://flutter.dev/docs/testing/debugging
flutter doctor -v
[✓] Flutter (Channel stable, 1.22.6, on macOS 11.1 20C69 darwin-x64, locale en-GB)
    • Flutter version 1.22.6 at /Users/tahatesser/Code/flutter_stable
    • Framework revision 9b2d32b605 (9 days ago), 2021-01-22 14:36:39 -0800
    • Engine revision 2f0af37152
    • Dart version 2.10.5

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Volumes/Extreme/SDK
    • Platform android-30, build-tools 30.0.3
    • ANDROID_HOME = /Volumes/Extreme/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-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.4)
    • Xcode at /Volumes/Extreme/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    • CocoaPods version 1.10.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-6915495)

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

[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.

Thank you

@TahaTesser TahaTesser added in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds labels Feb 1, 2021
@S4b3
Copy link
Author

S4b3 commented Feb 1, 2021

Sadly the official example doesn't cover my issue.
As for what I managed to understand during the past few days, having this as a webview:

return WebView(
       initialUrl: 'myapp://?code',
       userAgent: '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',
       javascriptMode: JavascriptMode.unrestricted,
       onWebViewCreated: (WebViewController webViewController) {
         _webViewController.complete(webViewController);
       },
       javascriptChannels: <JavascriptChannel>[
         _toasterJavascriptChannel(context),
       ].toSet(),
       navigationDelegate: (NavigationRequest request) {
         if (request.url.startsWith('https://www.youtube.com/')) {
           print('blocking navigation to $request}');
           return NavigationDecision.prevent;
         }
         print('allowing navigation to $request');
         return NavigationDecision.navigate;
       },
       onPageStarted: (String url) {
         print('Page started loading: $url');
       },
       onPageFinished: (String url) {
         print('Page finished loading: $url');
       },
       gestureNavigationEnabled: true,
     );
   }

Which only differs from the official example for the initialUrl, reproduces my problem.
Apparently Android's WebView manages to call the onPageFinished callback regardless if the Navigation was prevented or if the url hasn't led to an actual page; which to me sounds about right, since the page was infact loaded.

On iOS, instead, the method gets called way less (never, in this particular occasion) which ends up causing my problem.
For example, with the webview I quoted above this is the android console log:

I/flutter (14681): Page started loading: myapp://?code
I/flutter (14681): Page finished loading: myapp://?code

And this is iOS' :

flutter: allowing navigation to NavigationRequest(url: myapp://?code, isForMainFrame: true)
flutter: Page started loading: myapp://?code

Basically if loading the page would lead to an error or if the navigation was prevented the onPageFinished callback doesn't get called on iOS, which is a completely different behavior than android's and is causing me a massive headache.

Is there any way to manually handle this case?

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Feb 1, 2021
@TahaTesser
Copy link
Member

TahaTesser commented Feb 2, 2021

Hi @S4b3
Thanks for the details, I can reproduce the invalid initialUrl

With invalid URL on iOS

[  +44 ms] flutter: Page started loading: myapp://?code

With invalid URL on Android

[  +20 ms] I/flutter ( 9566): Page started loading: myapp://?code
[  +14 ms] I/flutter ( 9566): Page finished loading: myapp://?code
code sample
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'dart:async';

void main() => runApp(MyApp());

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final Completer<WebViewController> _webViewController =
      Completer<WebViewController>();

  final String _url = 'https://pub.dev';

  @override
  void initState() {
    super.initState();
    // Enable hybrid composition.
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  Widget build(BuildContext context) => Material(
        child: Center(
          child: WebView(
            initialUrl: 'myapp://?code',
            // initialUrl: _url,
            userAgent: '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',
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController webViewController) {
              _webViewController.complete(webViewController);
            },
            javascriptChannels: <JavascriptChannel>[
              // _toasterJavascriptChannel(context),
            ].toSet(),
            navigationDelegate: (NavigationRequest request) {
              if (request.url.startsWith('https://www.youtube.com/')) {
                print('blocking navigation to $request}');
                return NavigationDecision.prevent;
              }
              print('allowing navigation to $request');
              return NavigationDecision.navigate;
            },
            onPageStarted: (String url) {
              print('Page started loading: $url');
            },
            onPageFinished: (String url) {
              print('Page finished loading: $url');
            },
            gestureNavigationEnabled: true,
          ),
        ),
      );
}
flutter doctor -v
[✓] Flutter (Channel stable, 1.22.6, on macOS 11.1 20C69 darwin-x64, locale en-GB)
    • Flutter version 1.22.6 at /Users/tahatesser/Code/flutter_stable
    • Framework revision 9b2d32b605 (10 days ago), 2021-01-22 14:36:39 -0800
    • Engine revision 2f0af37152
    • Dart version 2.10.5

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Volumes/Extreme/SDK
    • Platform android-30, build-tools 30.0.3
    • ANDROID_HOME = /Volumes/Extreme/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-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.4)
    • Xcode at /Volumes/Extreme/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    • CocoaPods version 1.10.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-6915495)

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

[✓] Connected device (2 available)
    • Android SDK built for x86 (mobile) • emulator-5554             • android-x86 • Android 10 (API 29) (emulator)
    • Taha’s iPad (mobile)               • 00008020-000255113EE8402E • ios         • iOS 14.4

! Doctor found issues in 1 category.
[✓] Flutter (Channel master, 1.26.0-18.0.pre.124, on macOS 11.1 20C69 darwin-x64, locale en-GB)
    • Flutter version 1.26.0-18.0.pre.124 at /Users/tahatesser/Code/flutter_master
    • Framework revision 0a4618943c (37 minutes ago), 2021-02-02 14:56:03 +0800
    • Engine revision bb2bbde4d0
    • Dart version 2.12.0 (build 2.12.0-279.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Volumes/Extreme/SDK
    • Platform android-30, build-tools 30.0.3
    • ANDROID_HOME = /Volumes/Extreme/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-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Volumes/Extreme/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    • CocoaPods version 1.10.1

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

[✓] Android Studio (version 4.1)
    • 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 1.8.0_242-release-1644-b3-6915495)

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

[✓] Connected device (4 available)
    • Android SDK built for x86 (mobile) • emulator-5554             • android-x86    • Android 10 (API 29) (emulator)
    • Taha’s iPad (mobile)               • 00008020-000255113EE8402E • ios            • iOS 14.4
    • macOS (desktop)                    • macos                     • darwin-x64     • macOS 11.1 20C69 darwin-x64
    • Chrome (web)                       • chrome                    • web-javascript • Google Chrome 88.0.4324.96

• No issues found!

Thank you

@TahaTesser TahaTesser changed the title [webview_flutter] WebView doesn't launch onPageFinished on iOS [webview_flutter] onPageFinished never gets called on iOS with an invalid URL Feb 2, 2021
@TahaTesser TahaTesser added found in release: 1.22 Found to occur in 1.22 found in release: 1.26 Found to occur in 1.26 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: first party p: webview The WebView plugin platform-ios iOS applications specifically and removed in triage Presently being triaged by the triage team labels Feb 2, 2021
@cyanglaz cyanglaz added the P2 Important issues not at the top of the work list label Feb 4, 2021
@danagbemava-nc
Copy link
Member

I can reproduce the issue on master 2.5.0-7.0.pre.83 and stable 2.2.3 with webview_flutter: ^2.0.12.

On android navigation happens and the page throws an error, however on iOS, the screen is just blank.

iOS logs
flutter: allowing navigation to NavigationRequest(url: myapp://?code, isForMainFrame: true)
flutter: Page started loading: myapp://?code

Simulator Screen Shot - iPhone 12 - 2021-08-16 at 07 45 37

android logs
I/flutter ( 5563): Page started loading: myapp://?code
I/flutter ( 5563): Page finished loading: myapp://?code

photo_2021-08-16 07 48 43

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

void main() => runApp(MyApp());

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final Completer<WebViewController> _webViewController =
      Completer<WebViewController>();

  final String _url = 'https://pub.dev';

  @override
  void initState() {
    super.initState();
    // Enable hybrid composition.
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  Widget build(BuildContext context) => Material(
        child: Center(
          child: WebView(
            initialUrl: 'myapp://?code',
            // initialUrl: _url,
            userAgent: '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',
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController webViewController) {
              _webViewController.complete(webViewController);
            },
            javascriptChannels: <JavascriptChannel>[
              // _toasterJavascriptChannel(context),
            ].toSet(),
            navigationDelegate: (NavigationRequest request) {
              if (request.url.startsWith('https://www.youtube.com/')) {
                print('blocking navigation to $request}');
                return NavigationDecision.prevent;
              }
              print('allowing navigation to $request');
              return NavigationDecision.navigate;
            },
            onPageStarted: (String url) {
              print('Page started loading: $url');
            },
            onPageFinished: (String url) {
              print('Page finished loading: $url');
            },
            gestureNavigationEnabled: true,
          ),
        ),
      );
}
flutter doctor -v
[✓] Flutter (Channel master, 2.5.0-7.0.pre.83, on macOS 11.5.1 20G80 darwin-arm,
    locale en-GH)
    • Flutter version 2.5.0-7.0.pre.83 at /Users/nexus/dev/sdks/flutters
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 06be7e5392 (11 hours ago), 2021-08-15 14:37:02 -0400
    • Engine revision 1af0a20793
    • Dart version 2.15.0 (build 2.15.0-15.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 (1 available)
    • 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 (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 92.0.4515.131

• No issues found!

@lielvan
Copy link

lielvan commented Feb 20, 2022

Any solutions or workarounds to this?
I'm currently experiencing a similar issue with onPageFinished never being called on iOS but working just fine on Android (and this is with a valid url...).

@bhayatus
Copy link

bhayatus commented Mar 4, 2022

Also getting this issue for a valid url, in my case onPageStarted is called on Android, but not iOS.

@lllyyy
Copy link

lllyyy commented Apr 26, 2022

iOS除了第一个url,在h5中跳转其他url无法触发onPageFinished方法

@yogeshButani
Copy link

WebView(
initialUrl: widget.txn_data.transaction!.url,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller = webViewController;
},
onPageFinished: (finish) async {
final response = await _controller!
.runJavascriptReturningResult(
"document.documentElement.innerText");
print("response is >>>>>>>>>>${jsonDecode(response)}");
Map<String, dynamic> jsonResponse =
convert.jsonDecode(jsonDecode(response));
successTxnModel responsemain =
successTxnModel.fromJson(jsonResponse);
if (responsemain.status == true) {
UtilityHlepar().getToast('Payment Success');

              showCustomDialog(context, popup());
              print('Success');
            } else {
              print('output is ${responsemain.message}');
              print('status is ${responsemain.status}');
              print('data is ${responsemain.data}');
              Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(
                      builder: (context) => Dashboard_Screen()),
                  (route) => false);
              UtilityHlepar().getToast('Payment Failed');
            }
          },
        ),

Same issue, if else condition not calling on ios, works perfect on android...what is the solution of this?

@yogeshButani

This comment was marked as duplicate.

@yogeshButani

This comment was marked as duplicate.

@yogeshButani
Copy link

Sadly the official example doesn't cover my issue. As for what I managed to understand during the past few days, having this as a webview:

return WebView(
       initialUrl: 'myapp://?code',
       userAgent: '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',
       javascriptMode: JavascriptMode.unrestricted,
       onWebViewCreated: (WebViewController webViewController) {
         _webViewController.complete(webViewController);
       },
       javascriptChannels: <JavascriptChannel>[
         _toasterJavascriptChannel(context),
       ].toSet(),
       navigationDelegate: (NavigationRequest request) {
         if (request.url.startsWith('https://www.youtube.com/')) {
           print('blocking navigation to $request}');
           return NavigationDecision.prevent;
         }
         print('allowing navigation to $request');
         return NavigationDecision.navigate;
       },
       onPageStarted: (String url) {
         print('Page started loading: $url');
       },
       onPageFinished: (String url) {
         print('Page finished loading: $url');
       },
       gestureNavigationEnabled: true,
     );
   }

Which only differs from the official example for the initialUrl, reproduces my problem. Apparently Android's WebView manages to call the onPageFinished callback regardless if the Navigation was prevented or if the url hasn't led to an actual page; which to me sounds about right, since the page was infact loaded.

On iOS, instead, the method gets called way less (never, in this particular occasion) which ends up causing my problem. For example, with the webview I quoted above this is the android console log:

I/flutter (14681): Page started loading: myapp://?code
I/flutter (14681): Page finished loading: myapp://?code

And this is iOS' :

flutter: allowing navigation to NavigationRequest(url: myapp://?code, isForMainFrame: true)
flutter: Page started loading: myapp://?code

Basically if loading the page would lead to an error or if the navigation was prevented the onPageFinished callback doesn't get called on iOS, which is a completely different behavior than android's and is causing me a massive headache.

Is there any way to manually handle this case?

how did you solve the issue? please help me

@BunnyBuddy

This comment was marked as duplicate.

@yogeshButani

This comment was marked as duplicate.

@BunnyBuddy
Copy link

This is the main heading of the issue

io.flutter.plugins.webviewflutter.InputAwareWebView$1.run

This is what i have in Google Play Console crash logs

Exception java.lang.NullPointerException: Attempt to read from field 'android.view.WindowManager$LayoutParams android.view.ViewRootImpl.mWindowAttributes' on a null object reference
  at android.view.inputmethod.InputMethodManager.startInputInner (InputMethodManager.java:1662)
  at android.view.inputmethod.InputMethodManager.checkFocus (InputMethodManager.java:1901)
  at android.view.inputmethod.InputMethodManager.isActive (InputMethodManager.java:1220)
  at io.flutter.plugins.webviewflutter.InputAwareWebView$1.run (InputAwareWebView.java)
  at android.os.Handler.handleCallback (Handler.java:888)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loop (Looper.java:213)
  at android.app.ActivityThread.main (ActivityThread.java:8178)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:513)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1101)

@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-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team labels Jul 8, 2023
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 P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. platform-ios iOS applications specifically team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team
Projects
Development

No branches or pull requests