Sending actions and data objects from JavaScript code to native code
IBM MobileFirst™ Platform Foundation lets you send actions with optional data objects from JavaScript to C#, iOS or Java™ code.
About this task
You might want to send a custom action from JavaScript code to native code, for example, for updating the user interface.
Any object can receive actions. To do so, it must implement the WLActionReceiver interface (for Android, Windows Phone Silverlight 8) or protocol (for iOS).- Android
-
void onActionReceived (String action, JSONObject data);
Examplepublic class MyReceiver implements WLActionReceiver{ void onActionReceived(String action, JSONObject data){ //process received action } }
Note: Actions are always delivered on a background thread. If you want to update the application user interface from the received action, do so on a main user interface thread, for example by using the Context.runOnUIThread method. - iOS
-
-(void)actionReceived:(NSString*)action withData:(NSDictionary*)data;
Example// MyReceiver.h file #import "WLActionReceiver.h"; @interface MyReceiver: NSObject <WLActionReceiver>{} @end // MyReceiver.m file @implementation MyReceiver -(void)onActionReceived:(NSString *)action withData:(NSDictionary *)data{ // process received action } @end
Note: Actions are always delivered on a background thread. If you want to update the application user interface from the received action, do so on a main user interface thread, for example by using the performSelectorOnMainThread method. - Windows Phone Silverlight 8
-
void onActionReceived (String action, JObject data)
Examplepublic class MyReceiver : WLActionReceiver { public void onActionReceived(string action, JObject data) { //process received action } }
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.