Skip to content

WebView JavaScriptChannelHandler didReceiveScriptMessage should JSON-encode collections #42986

@KennedyChiang

Description

@KennedyChiang

Problem

Now JavaScriptChannelHandler.m - (void)userContentController:didReceiveScriptMessage: arguments receive Dictionary or Array will got a string that hard to serialized like below.

{
    action = do_somethineg;
    data =     {
        key_1 = some_info;
        key_2 = "some_info";
    };
    timestamp = 1570005270866;
    token = "jwt_token";
}

Suggestion to modify

   NSString *body;
   if ([message.body isKindOfClass:[NSDictionary class]] ||
       [message.body isKindOfClass:[NSArray class]]) {
       NSData *jsonData = [NSJSONSerialization dataWithJSONObject:message.body
                                                          options:NSJSONWritingPrettyPrinted
                                                            error:nil];
       body = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
   }
   if (body.length <= 0) {
       body = [NSString stringWithFormat:@"%@", message.body];
   }

   NSDictionary* arguments = @{
     @"channel" : _javaScriptChannelName,
     @"message" : body
   };
   [_methodChannel invokeMethod:@"javascriptChannelMessage" arguments:arguments];

hope it will getting update soon 👍🏼

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: proposalA detailed proposal for a change to Flutterp: requires breaking changeA change that should be batched into the next breaking change to this packagep: webviewThe WebView pluginpackageflutter/packages repository. See also p: labels.platform-iosiOS applications specificallyteam-iosOwned by iOS platform teamtriaged-iosTriaged by iOS platform team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions