preface

In some scenarios, you need to pass parameters to the default receiving page (Pages /index/index) of the applet.

This article will take passing the name and PWD parameters as an example to describe the implementation process of this scenario in Android and iOS applets respectively.

The premise condition

The applets component has been plugged in as per the Quick Start documentation.

Android applet

1. Add parameters for the startup page of the client. As follows:

Bundle param = new Bundle(); String query = "name="+Uri.encode("123")+"&pwd="+Uri.encode("456"); param.putString("query",query); // Set the MPNebula. StartApp (appId:"2020121620201216",param);Copy the code

When the URL starts parameter transmission, the parameter field is query. The parameters are obtained by parsing the Query field. StartApp Parameter description:

  • AppId: The ID of the applets, which can be viewed from the mPaaS console.

  • Param: Bundle object, which can pass request parameters to the Bundle object, key=”query”,value=” key/value pair “; Multiple parameters are separated by (&).

  • Note 1: The applets framework uri decode the value of each key-value pair of custom input parameters. Therefore, uri encode is performed on the value of the input parameter key value pair.

  • Note 2: The applets framework does nothing to the key of the key-value pair of the custom input parameter. Therefore, do not set special characters for the key to prevent the small program side from identifying the user-defined parameters.

2. Applets get parameters. From options in the onLaunch/onShow(options) method.

Storing app.js takes the parameters passed by the client to the applet and stores them in the global variable globalData, from which they can be used directly or updated. Parameters such as the token and user_id in the request header are stored in globalData after being sent from Native.

A small program that iOS

1. Add parameters for the startup page of the client. As follows:

NSString *pwd = [@"123&*! @#$%^*" stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"?! @#$^&%*+,:;='\"`<>()[]{}/\\| "] invertedSet]]; NSString *queryvalue = [NSString stringWithFormat:@"name=mpaas&pwd=%@",pwd]; NSDictionary * dic = @{@"query":queryvalue}; [MPNebulaAdapterInterface startTinyAppWithId:@"1234567891234567" params:dic];Copy the code

When the URL starts parameter transmission, the parameter field is query. The parameters are obtained by parsing the Query field. StartApp Parameter description:

  • AppId: the ID of the applets, obtained from the mPaaS console.

  • Param: params applet parameter, use @{@”query”:@”key=value&key=value”}; Multiple parameters are separated by &.

  • Note 1: The applets framework decode the value of each key-value pair of custom input parameters. If your input parameter key value pair has a special character & in its value, call the following method to encode the input parameter. NSString pwd = [@”123&! @# %^*” stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@”?! @#^&%*+,:;='”`<>()[]{}/\| “] invertedSet]];

If there are no special characters, you do not need to use encode.

  • Note 2: The applets framework does nothing to the key of the key-value pair of the custom input parameter. Therefore, do not set special characters for the key to prevent the small program side from identifying the user-defined parameters.

2. The applets get parameters from the options parameter of the onLaunch/onShow(options) method.

The operation method is the same as that of android.

Liu Qiyang, Teng Hongcai

E.N.D.