Skip to content

[webview_flutter] Can not invoke Javascript function on iOS #128489

@TarunRaxa

Description

@TarunRaxa

Is there an existing issue for this?

Steps to reproduce

This package work perfectly fine on android but for ios I am getting crash when I runJavaScript from webview. This happens only on iOS Side. For android I am able to send and receive data between js and flutter.

I am using latest version of package.

And the error at ios side is -

Unhandled Exception: PlatformException(FWFEvaluateJavaScriptError, Failed evaluating JavaScript., Instance of 'NSError', null)
#0      WKWebViewHostApiImpl.evaluateJavaScriptForInstances (package:webview_flutter_wkwebview/src/web_kit/web_kit_api_impls.dart:1050:7)
<asynchronous suspension>
#1      WebKitWebViewController.runJavaScript (package:webview_flutter_wkwebview/src/webkit_webview_controller.dart:385:7)
<asynchronous suspension>
#2      _WebViewExampleState.build.<anonymous closure> (package:my_rich_editor/web_view.dart:252:11)
<asynchronous suspension>

Expected results

Expected result I should runJavaScript from ios side too.

Actual results

Actually it is not sending data from js to flutter

Code sample

Code sample
WebViewController controller = WebViewController();
controller
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(const Color(0x00000000))
    ..addJavaScriptChannel(
      'GetAppCurrentLanguage',
      onMessageReceived: (JavaScriptMessage message) {
        print('GetAppCurrentLanguage');
        String languageCode = "en";
             },
    )
   ..loadFlutterAsset('assets/demo.html');

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
Unhandled Exception: PlatformException(FWFEvaluateJavaScriptError, Failed evaluating JavaScript., Instance of 'NSError', null)
#0      WKWebViewHostApiImpl.evaluateJavaScriptForInstances (package:webview_flutter_wkwebview/src/web_kit/web_kit_api_impls.dart:1050:7)
<asynchronous suspension>
#1      WebKitWebViewController.runJavaScript (package:webview_flutter_wkwebview/src/webkit_webview_controller.dart:385:7)
<asynchronous suspension>
#2      _WebViewExampleState.build.<anonymous closure> (package:my_rich_editor/web_view.dart:252:11)
<asynchronous suspension>

Flutter Doctor output

Doctor output
Flutter (Channel stable, 3.10.3, on macOS 13.0.1 22A400 darwin-arm64, locale en-IN)
[!] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.78.2)
[✓] Connected device (3 available)
[✓] Network resources

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions