ios - 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<…>
我的 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";');
}
},
),
));
}
解决方案
尝试将此添加到您的 info.plist 中,替换您的支付网关的域
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>yourdomain.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
并且不要忘记重新安装应用程序
推荐阅读
- zend-server - 在 Zend Server Developer Edition 9 中更改 max_execution_time
- sql - 在 SQL 中实现 for 循环以更新行
- java - 如何在java中将日期对象从一种格式转换为另一种格式
- forms - 如何使用 Vue.js 将 props 传递给表单中的 URL
- python - 如何为此功能提供硬编码文件?
- c# - 寻找一天的平均值
- java - 多维数组的问题
- reactjs - TS2339:属性 'defaultProps' 不存在于类型 '(props: any) => detailedReactHTMLElement<{ className: string; }, HTML元素>'
- go - readString 与 readLine
- java - 我可以将扩展类中的变量类型强制为 Java 中的子类型吗?