gpt4 book ai didi

Flutter InAppWebView throwing error Uncaught (in promise) NotAllowedError: Write permission denied. when tested on Android devices(Ffltter InAppWebView引发错误未捕获(在Promise中)NotAlledError:写入权限被拒绝。在Android设备上测试时)

转载 作者:bug小助手 更新时间:2023-10-24 19:02:19 39 4
gpt4 key购买 nike



Objective

客观化



  • I am trying to copy a text from a copy button inside a web page. The copy function itself is working fine when i open it using chrome, firefox and safari browser


Expected behavior:
After pressing the copy button, the text is copied into phone clipboard.
Current behavior:
After pressing the copy button, Flutter InAppWebView throwing error Uncaught (in promise) NotAllowedError: Write permission denied.

预期行为:按下复制按钮后,文本将复制到手机剪贴板中。当前行为:按下复制按钮后,Ffltter InAppWebView抛出错误未捕获(在Promise中)NotAlledError:写入权限被拒绝。


Step done to fix this error

修复此错误的步骤已完成



  1. I have added android permission inside my Project > android > app > src > main >AndroidManifest.xml


    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


  1. PermissionRequestResponseAction is set to .GRANT, however, this callback is not called when i press on the copy button inside the web page


    androidOnPermissionRequest: (webViewController, origin, resources) async {
return PermissionRequestResponse(
resources: resources,
action: PermissionRequestResponseAction.GRANT,
);
},



  1. javaScriptEnabled is set to true for Android, and its true by default


  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
// allowFileAccessFromFileURLs: true,
// javaScriptCanOpenWindowsAutomatically: true,
// allowUniversalAccessFromFileURLs: true,
javaScriptEnabled: true,
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: false),
android: AndroidInAppWebViewOptions(
useHybridComposition: true,
),
ios: IOSInAppWebViewOptions(
allowsInlineMediaPlayback: true,
));


Here are some related issue that have been asked, but still no solution for it:

以下是被问到的一些相关问题,但仍然没有解决方案:



  1. webview_flutter: Uncaught (in promise) NotAllowedError: Write permission denied



  2. https://github.com/pichillilorenzo/flutter_inappwebview/issues/678




How to solve for this error? Any help is much appreciated, thank you

如何解决这个错误?非常感谢您的任何帮助,谢谢


更多回答
优秀答案推荐

i also face this issue so i was resolve it by this way:

我也面临着这个问题,所以我通过这样的方式解决了它:



  • i was try to add permission for android but it still not work and then i change to use java script to solve it maybe u can see it:


InAppWebView(
initialUrlRequest: URLRequest(url: Uri.parse(url)),
onLoadStop: (controller, url) {
controller
..evaluateJavascript(
source:
"document.getElementById('unwanted-element').remove();",
)
..evaluateJavascript(
source:
"document.body.style.background = 'linear-gradient(45deg, #3D1912, #701D1B)';",
)
..evaluateJavascript(
source:
'javascript:navigator.clipboard.writeText = (msg) => { return window.flutter_inappwebview?.callHandler("android", msg); }');
},
onWebViewCreated: (controller) async {
if (Platform.isAndroid) {
handleCopyText(controller, context, 'android');
} else {
handleCopyText(controller, context, 'copyText');
}
},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
disableVerticalScroll: true, disableHorizontalScroll: true),
),
);

and then u can listen callback from webview to catch your text and save it to clipboard

然后你可以收听Webview的回调来捕捉你的文本并保存到剪贴板



Looks like the proper solution to handle such cases is what @ThanhOP mentioned, The only fix that is working is to handle that with JS injection.
Register a channel to get the data and send It to the FLutter side.

看起来处理这种情况的正确解决方案就是@ThanhOP提到的,唯一有效的修复方法是使用JS注入来处理。注册一个通道以获取数据并将其发送到颤振侧。


Note: I deleted extra code and added the required ones.
It works like a charm even with websites that had no problem.

注意:我删除了多余的代码,添加了必需的代码。即使在没有问题的网站上,它也像一个护身符一样起作用。


onLoadStop property looks like this:

OnLoadStop属性如下所示:


    onLoadStop: (controller, url) {
controller.evaluateJavascript(
source:
'javascript:navigator.clipboard.writeText = (msg) => { return window.flutter_inappwebview?.callHandler("axs-wallet-copy-clipboard", msg); }');
controller.addJavaScriptHandler(
handlerName: 'axs-wallet-copy-clipboard',
callback: (args) {
copy(args);
},
);
},

copy function looks like this:

复制功能如下所示:


  void copy(List<dynamic> params) {
Clipboard.setData(ClipboardData(text: params[0]));
}

更多回答

Please integrate your code as part of your answer rather than via a link to a screenshot. To do this, refer to the section Code Blocks on the help page for formatting on StackOverflow.

请将您的代码整合为您的答案的一部分,而不是通过指向屏幕截图的链接。为此,请参考帮助页上的代码块一节以了解StackOverflow上的格式设置。

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

正如它目前所写的,你的答案并不清楚。请编辑以添加更多详细信息,以帮助其他人了解这是如何解决提出的问题的。你可以在帮助中心找到更多关于如何写出好答案的信息。

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com