Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evalJavascript inconsistent between Android and iOS. #126

Open
manujbahl opened this issue Jul 14, 2018 · 3 comments
Open

evalJavascript inconsistent between Android and iOS. #126

manujbahl opened this issue Jul 14, 2018 · 3 comments
Labels

Comments

@manujbahl
Copy link

the response from evalJavascript on Android is adding a set of " at the beginning and end of the returned string.

iOS response - xyz
android response - "xyz"

@va-andrew
Copy link

+1 for this issue. iOS also has weird behavior like returning 1 for true, while Android is more typical by returning a JSON string of the result. I've been using the following to retrieve an object from an evalJavascript() statement, but it would be nice to have something less hacky/more official:

bool jsRequiresStringify;
Future evalAndParseJS(FlutterWebviewPlugin plugin, String expr) async {
  if (jsRequiresStringify == null) {
    final String boolTest = await plugin.evalJavascript('true');
    final String intTest = await plugin.evalJavascript('0');
    final String stringTest = await plugin.evalJavascript('"hello"');
    if ((boolTest == 'true') && (intTest == '0') && (stringTest == '"hello"')) {
      // Android
      jsRequiresStringify = false;
    } else if ((boolTest == '1') && (intTest == '0') && (stringTest == 'hello')) {
      // iOS
      jsRequiresStringify = true;
    } else {
      // Unknown behavior
      jsRequiresStringify = true;
    }
  }
  final String fullExpr = jsRequiresStringify ? 'JSON.stringify($expr)' : expr;
  final String resultStr = await plugin.evalJavascript(fullExpr);
  try {
    return jsonDecode(resultStr);
  } catch(e) {
    print('jsonDecode error! $e');
    return null;
  }
}

@lejard-h lejard-h added the bug label Aug 5, 2018
@lejard-h
Copy link
Collaborator

lejard-h commented Aug 5, 2018

Indeed iOS has a strange behavior on this. How do you determine if the 1 you get, is the result of a boolean or number ?

For your function, you can use Platform.isIOS and Platform.isAndroid instead of testing evalJavascript.

I could add it to the plugin but it does not seem to be a good solution for now.

@ASemeniuk
Copy link

The problem actually has greater scope than flutter_webview_plugin. It seems that JavaScriptChannel behaves differently on these platforms. In my projects I am forced to use hacks as ones suggested above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants