首页 > 解决方案 > Flutter WebView 在 iOS 上不会调用 onPageFinished

问题描述

我正在使用webview_flutter以通过 Cognito 的身份验证端点对我的用户进行身份验证。

这是我的网络视图:

    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,
      );

当 url 包含我的身份验证代码时阻止导航允许我在 onPageFinished 和Android 上执行魔法,一切正常。

另一方面,在 iOS 上,onPageFinished 回调永远不会被调用。打印永远不会出现在控制台中,并且导航会在验证码处停止而没有任何操作。这就是我得到的:

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

而在每次导航 onPageFinished 之后都应该被调用。我真的不知道如何或为什么会发生这种情况;我搜索了很多问题,我当前的 Info.plist 包含:

    <key>io.flutter.embedded_views_preview</key>
    <string>YES</string>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key> <true/>
        <key>NSAllowsArbitraryLoadsInWebContent</key> <true/>
    </dict>

(我会确保在完成此操作后不会翻倍)这是我的颤振医生-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.

(我不再使用 IntelliJ 了)

什么可能导致此问题?请发送帮助

标签: androidiosflutterwebviewamazon-cognito

解决方案


推荐阅读