'Flutter WebView doesn't call onPageFinished on iOS

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. 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>
    <string>YES</string>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key> <true/>
        <key>NSAllowsArbitraryLoadsInWebContent</key> <true/>
    </dict>

(Which i'll make sure not to leave doubled after I'm done with this) 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)

What could be causing this issue? Pls send help



Solution 1:[1]

Better use this one https://inappwebview.dev/docs/in-app-webview/basic-usage/

flutter_inappwebview: ^5.3.2

it works for me.

Solution 2:[2]

So I just recently came back to this and since nobody found a solution I'll write down what I remember from solving the problem:

In Flutter the web_view package makes use of platform native webviews, which have different behaviour between Android and iOS:

Android proceeds to call onPageFinished every time a page is loaded, regardless of what the response is; iOS, instead calls said method if and only if the page is returned correctly (response code 200).

To solve this issue during testing I simply modified the url I was redirecting the auth to the company website I was working for, effectively giving me an intermediate step to retrieve the auth code from the url params.

Not the best of the solutions, but surely one that worked. In the meanwhile I believe I also opened an issue on github, which has still to be solved. Here

Happy Coding everyone! o/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Humza Shahid
Solution 2 Sabe