首页 > 解决方案 > Flutter Webview Apple Pay

问题描述

在尝试使用 WebViewController.evaluateJavascript('myjs') 时,我在调试控制台中收到此错误。

[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: PlatformException(evaluateJavaScript_failed, Failed evaluating JavaScript, JavaScript string was: 'doucment.getElementById("checkout_email").value = "Hell0";'
Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=0, WKJavaScriptExceptionMessage=Unable to run user agent scripts because this document has previously accessed Apple Pay. Documents can be prevented from accessing Apple Pay by adding a WKUserScript to the WKWebView's WKUserContentController., WKJavaScriptExceptionColumnNumber=0, NSLocalizedDescription=A JavaScript exception occurred})
#0      StandardMethodCodec.decodeEnvelope 
package:flutter/…/services/message_codecs.dart:572
#1      MethodChannel._invokeMethod 
package:flutter/…/services/platform_channel.dart:161
<asynchronous suspension>
#2      MethodChannel.invokeMethod 
package:flutter/…/services/platform_channel.dart:334
#3      MethodChannelWebViewPlatform<…&gt;

我的 Dart 文件看起来像这样,在页面加载时,我做了一个简单的检查,以确保我在正确的页面上执行 js。如果检查通过,则执行 js,但不幸返回上述错误。我试过在谷歌上寻找答案,但没有发现与我遇到的问题类似的东西。

  final Completer<WebViewController> _controller =
      Completer<WebViewController>();

  TextEditingController _searchQueryController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: _searchBar(),
        leading: _directionButton('backward'),
        actions: [_directionButton('forward')],
      ),
      body: WebView(
        initialUrl: 'https://google.com',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) {
          _controller.complete(webViewController);
        },
        onPageStarted: (url) {
          _searchQueryController.text = url;
        },
        onPageFinished: (url) async {
          print('Page Finished: ' + url);
          final controller = await _controller.future;
          if (url.contains('checkouts')) {
            await controller.evaluateJavascript('doucment.getElementById("checkout_email").value = "Hello";');
          }
        },
      ),
    ));
  }

标签: iosflutterdartuiwebviewwkwebview

解决方案


尝试将此添加到您的 info.plist 中,替换您的支付网关的域

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>yourdomain.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

并且不要忘记重新安装应用程序


推荐阅读