Pass startup parameters to an iOS mini program
This topic describes how to pass parameters to the default landing page of a mini program (pages/index/index). The `name` and `pwd` parameters are used as an example.
Prerequisites
For instructions on how to integrate the Mini Program component, see the Getting Started document.
Procedure
In the client, add the parameters for the startup page 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];The field for passing parameters is `query`. To retrieve the parameters, parse the `query` field.
Parameters for startApp:
`appId`: The ID of the mini program. You can obtain this ID from the mPaaS console.
`param`: The parameters for the mini program. To pass custom values, use
@{@"query":@"key=value&key=value"}. Separate multiple parameters with an ampersand (&).
ImportantThe mini program framework decodes the `value` of each custom key-value input parameter. If the `value` of a key-value pair contains special characters, such as
&, call the following method to encode the input parameter.NSString *pwd = [@"123&*!@#$%^*" stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "] invertedSet]];If the value contains no special characters, encoding is not required.The mini program framework does not process the `key` of any custom key-value input parameter. Do not use special characters in the `key`. Otherwise, the mini program may not recognize the custom parameters.
Retrieve parameters from the `options` argument of the `onLaunch/onShow(options)` method. In `app.js`, retrieve the parameters passed from the client and save them to the `globalData` global variable. To use the parameters, you can retrieve or update their values directly from `globalData`. For example, you can pass parameters such as `token` and `user_id` from the native client, save them to `globalData`, and then retrieve them directly when needed.
Use startup parameters to navigate to a specific mini program page
If a mini program has multiple pages, you can open it to a specific page at launch. Use the following code:
NSDictionary * dict = @{
@"page" : @"/pages/demo3/index"
};
[MPNebulaAdapterInterface startTinyAppWithId:@"1234567891234567" params:dict];