+

Search Tips   |   Advanced Search

Sending actions and data objects from native code to JavaScript code

IBM MobileFirst Platform Foundation lets you send actions with optional data objects from C#, iOS or Java code to JavaScript code.

You might want to send a custom action from native code to JavaScript code (for example, for updating the user interface).

Actions are received by action receivers. Actions that cannot be delivered immediately are queued by the MobileFirst framework and delivered as soon as a suitable action receiver is registered.

  1. Add action receiver function in JavaScript code. For example:

      WL.App.addActionReceiver ("MyActionReceiverId", actionReceiver);

  2. Send action from native code to JavaScript code. For example:

    Android

    JSONObject data = new JSONObject();
    data.put("someProperty", 12345);
    WL.getInstance().sendActionToJS("doSomething", data);

    iOS

    NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
    [data setValue:@"12345" forKey:@"testParam"];
    [[WL sharedInstance] sendActionToJS:@"nativeToJsWithParams" withData:data];  

    Windows Phone 8

    JObject data = {someProperty:1234};
    WL.getInstance().sendActionToJS("doSomething", data);

  3. Implement a JavaScript action receiver function to receive and handle incoming actions and data. For example:
    function actionReceiver(received){
      if (received.action === "doSomething" && received.data.someProperty === "12345"){ 
        //perform  required actions, e.g., update web user interface }


Parent topic: Sending actions and data objects between JavaScript code and native code