• flutter webview_flutter 与JavaScript交互


    使用webview_flutter iOS需要在xcode工程中info.plist文件添加如果字典

    <key>io.flutter.embedded_views_preview</key>
    <string>YES</string>
    

    webview_flutter与交互
    1.通过拦截url的方式

          navigationDelegate: (NavigationRequest navigation) {
            String url = navigation.url;
    
            if (url.contains("micrpayclient://")) {
              //之前定义的micrpayclient保持不变
              String lStrig = 'micrpayclient://url=';
              int index = url.indexOf(lStrig);
              String subString = url.substring(index + lStrig.length);
              try {
                var dUrl = Uri.decodeComponent(subString);
                launch(dUrl);
              } catch (error) {
                print(error);
              }
              return NavigationDecision.prevent;
            }
            return NavigationDecision.navigate;
          },
    

    2.通过JavascriptChannel来实现

     WebView(
          initialUrl: widget.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",
          navigationDelegate: listenWebview,
          javascriptMode: JavascriptMode.unrestricted,
          onWebViewCreated: (vc) {
            _controller = vc;
            print('webviewUrl = ${widget.url}');
          },
          onPageFinished: (String value) {
            print('webviewUrl = $value');
            _handleNaviData();
          },
          javascriptChannels: [
            JavascriptChannel(
            name: 'tudouApp',//handleName
            onMessageReceived: (JavascriptMessage message) {
              print(message.message);
              //接收到js返回的数据
              //自定义处理
            }),
            JavascriptChannel(
            name: 'JSHandle',//handleName
            onMessageReceived: (JavascriptMessage message) {
              print(message.message);
              //接收到js返回的数据
              //收到js返回并作出应答
             String callbackname = message.message; 
             String data = "收到消息调用了";
             String script = "$callbackname($data)";
             _controller.evaluateJavascript(script);
            }),
          ].toSet(),
        )
    
  • 相关阅读:
    【BZOJ4517】排列计数(排列组合)
    【BZOJ2733】永无乡(线段树,启发式合并)
    【BZOJ1237】配对(贪心,DP)
    【BZOJ1492】货币兑换Cash(CDQ分治)
    CDQ分治模板
    【BZOJ3932】任务查询系统(主席树)
    【BZOJ3295】动态逆序对(BIT套动态加点线段树)
    【BZOJ3626】LCA(树上差分,树链剖分)
    图书管理系统
    树集合,树映射
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/12633184.html
Copyright © 2020-2023  润新知