Worklight 6.2 share info between apps family

让人想犯罪 __ 提交于 2019-12-11 09:44:52

问题


In Worklight 6.2, you are able to share information between applications in the same family.

My understanding using this API you can share key value pairs between apps. Is it possible to set a complex JSON as the value? Or is it only possible to share a string?

I'm not sure about how you need to define that an applications belongs to an applications family - How is it configured? Is there a tutorial about this functionality?


回答1:


To enable simple data sharing in worklight 6.2

  1. Add an environment
  2. Edit the application descriptor file to enable simple data sharing between apps
  3. Specify the application family name
  4. save and build all environments

For more info refer Enabling simple data sharing in worklight 6.2

To use the simple data sharing in your app. You can use the following WL.Client Api's

  • setSharedToken(object)
  • getSharedToken(object)
  • clearSharedToken(object)

For more info refer WL.Client api for worklight 6.2

As of now i didn't find any tutorials for simple data sharing.

And to answer you question AFAIK you can use JSON object as value for your key. It is only available for Android and iOS environments.

UPDATE 18-July-2014 I Couldn't find any tutorial so created one for simple data shar between apps.




回答2:


IBM Worklight has a feature of "Enabling the Simple Data Sharing for hybrid applications" (i.e. Sharing the data between two or more apps which belongs to the same family)

BUT THIS DOESN'T WORK.

For setting the data we use "WL.Client.setSharedToken(object)", which takes JSON object as, object = { key : "abc", value : "12345" }; as the parameter. This sets the token in the device as a JSON object like {abc : "12345"}

and for retrieving the data we, the "WL.Client.getSharedToken(object)", which also takes a JSON object as, var object = { key : "abc" }; this returns the value of the key "abc" (i.e. "12345" in this case)

There is a bug in "WL.Client.getSharedToken(object)", which is used to fetch the stored information. In worklight.js (where all the client side APIs are defined) file, for fetching the data (when WL.Client.getSharedToken(object) is called), the call which is being made is asynchronous, but it should be synchronous.

Internally WL.Client.getSharedToken(object) is invoking a cordova function (for calling the native device feature for fetching the data which was stored by WL.Client.setSharedToken(object)), which is asynchronous, so it is not waiting for the device to return the stored value but returning the empty object.

It should be a synchronous call.

​this.getSharedToken = function(obj) { var dfd = $.Deferred();

  obj = obj || {};
  var key = obj.key || '';

  if (((WL.Client.getEnvironment() === WL.Env.IPHONE)
        || (WL.Client.getEnvironment() === WL.Env.IPAD)
        || (WL.Client.getEnvironment() === WL.Env.ANDROID))
        && (typeof cordova === 'object')) {
    setTimeout(function () {
      cordova.exec(dfd.resolve, dfd.reject, 'WLApp', 'getSharedToken', [key]);
    }, 0);
  } else {
    setTimeout(dfd.resolve, 0);
  }

  return dfd.promise();

};

I modified the above function in worklight.js in the android project which is created by worklight, when we build the environment

this.getSharedToken = function(obj) { var dfd = $.Deferred();

  obj = obj || {};
  var key = obj.key || '';

  if (((WL.Client.getEnvironment() === WL.Env.IPHONE)
        || (WL.Client.getEnvironment() === WL.Env.IPAD)
        || (WL.Client.getEnvironment() === WL.Env.ANDROID))
        && (typeof cordova === 'object')) {
    setTimeout(function () {
      cordova.exec(function(data){alert(JSON.stringify(data));}, function(res){alert(JSON.stringify(res))}, 'WLApp', 'getSharedToken', [key]);
    }, 0);
  } else {
    setTimeout(dfd.resolve, 0);
  }

  return dfd.promise();

};

so it first returned the empty object to the client side and then alerted the data which is fetched from the device (alert given in success callback of cordova.exec).

This is not the expected behavior. It should be a synchronous call i.e. once the data is fetched from the device then only its should be returned.



来源:https://stackoverflow.com/questions/24674477/worklight-6-2-share-info-between-apps-family

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!