init method

void init()

Implementation

void init() {
  if (WebViewPlatform.instance is BTWebKitWebViewPlatform) {
    params = WebKitWebViewControllerCreationParams(
      allowsInlineMediaPlayback: true,
      mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
    );
  } else {
    params = PlatformWebViewControllerCreationParams(
    );
  }

  final WebViewController controller = WebViewController.fromPlatformCreationParams(params);

  if(widget.userAgent != null) {
    controller.setUserAgent(widget.userAgent!);
  }

  String loadUrl = (widget.isWidget ?? false) ? WIDGET_URL : INAPP_URL;
  // print(loadUrl);

  controller
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(const Color(0x00000000))
    ..setNavigationDelegate(
      NavigationDelegate(
        onProgress: (int progress) {
          // debugPrint('WebView is loading (progress : $progress%)');
        },
        onPageStarted: (String url) {
          // debugPrint('Page started loading: $url');
        },
        onPageFinished: (String url) async {
          // debugPrint('Page finished loading: $url');
          (widget.isWidget ?? false) ? loadWidgetScript(url) : loadPaymentScript(url);

        },
        // onNavi
        onWebResourceError: (WebResourceError error) {
          debugPrint('''
          Page resource error:
          code: ${error.errorCode}
          description: ${error.description}
          errorType: ${error.errorType}
          isForMainFrame: ${error.isForMainFrame}
                  ''');
          if(error.errorCode == 3) { // SSL 인증서 에러, update 유도
            if(error.description.contains("sslerror:")) {
              if (this.widget.onError != null) {
                this.widget.onError!(error.description);
              }
              debounceClose();
            }
          }
        },
        onNavigationRequest: (NavigationRequest request) {
          if(request.url.contains("https://nid.naver.com")) {
            widget._controller.runJavaScript("document.getElementById('back').remove()");
          }
          return NavigationDecision.navigate;
        },
        // Navigation

      ),
    )
    ..addJavaScriptChannel(
      'BootpayCancel',
      onMessageReceived: onCancelJS,
    )
    ..addJavaScriptChannel(
      'BootpayError',
      onMessageReceived: onErrorJS,
    )
    ..addJavaScriptChannel(
      'BootpayClose',
      onMessageReceived: onCloseJS,
    )
    ..addJavaScriptChannel(
      'BootpayIssued',
      onMessageReceived: onIssuedJS,
    )
    ..addJavaScriptChannel(
      'BootpayConfirm',
      onMessageReceived: onConfirmJS,
    )
    ..addJavaScriptChannel(
      'BootpayDone',
      onMessageReceived: onDoneJS,
    )
    ..addJavaScriptChannel(
      'WidgetReady',
      onMessageReceived: onWidgetReadyJS,
    )
    ..addJavaScriptChannel(
      'WidgetResize',
      onMessageReceived: onWidgetResizeJS,
    )
    ..addJavaScriptChannel(
      'WidgetChangePayment',
      onMessageReceived: onWidgetChangePaymentJS,
    )
    ..addJavaScriptChannel(
      'WidgetChangeTerms',
      onMessageReceived: onWidgetChangeAgreeTermJS,
    )
    ..addJavaScriptChannel(
      'BootpayFlutterWebView',
      onMessageReceived: onRedirectJS,
    )
    ..loadRequest(Uri.parse(loadUrl));

  if (controller.platform is AndroidWebViewController) {
    AndroidWebViewController.enableDebugging(true);
    (controller.platform as AndroidWebViewController)
        .setMediaPlaybackRequiresUserGesture(false);
  }

  widget._controller = controller;
}