Flutter WebView doesn't call onPageFinished on iOS

437

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

flutter_inappwebview: ^5.3.2

it works for me.

Share:
437
Sabe
Author by

Sabe

Currently studing Engineering in University. Coding makes me happy, I have experience in C, Java, Assembly(Schoolarship) and currently facing C# and Xaml. Most Likely right know I'll be coding some sorta app.

Updated on December 27, 2022

Comments

  • Sabe
    Sabe over 1 year

    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

    • A. Shukri
      A. Shukri over 2 years
      anyone got the solution ? i also stuck on this
    • Sabe
      Sabe about 2 years
      I just added an answer to how i solved the issue at the time, sorry for having not done it earlier lol
  • Abhishek Dutt
    Abhishek Dutt about 2 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
  • Sabe
    Sabe about 2 years
    The link does not answer the question, since the problem causing this situation was a bit more complex to solve than changing a package. Assuming that said package still uses native webviews (which should be correct) it would not have solved the issue.. I wrote an answer that goes a bit more in depth