tencent cloud

All product documents
Tencent Cloud Super App as a Service
Custom Mini Program APIs
Last updated: 2025-03-17 15:45:40
Custom Mini Program APIs
Last updated: 2025-03-17 15:45:40
The SDK engine provides an extension mechanism that allows host apps to customise APIs for mini programs to call.

Implementation steps

1. Customise a class and import the TMAExternalJSPlugin.
#import <TCMPPSDK/TCMPPSDK.h>

@interface NativePluginTest : NSObject

@end
2. Declare TMA_REGISTER_EXTENAL_JSPLUGIN and add a custom API via TMAExternalJSAPI_IMP().

reference case

#import "NativePluginTest.h"
#import "TMAExternalJSPlugin.h"
#import "TMFMiniAppInfo.h" #import "TMFMiniAppInfo.h" #import "TMFMiniAppInfo.h".

Implementing NativePluginTest

TMA_register_extenal_jsplugin; //Custom sync api.

// custom sync api
TMAExternalJSAPI_IMP(testSync) {
TMFMiniAppInfo *appInfo = context.tmfAppInfo; NSDictionary *data = context.tmfAppInfo.
NSDictionary *data = params[@"data"];

NSLog(@"************ invokeNativePlugin testSync,appId:%@,data is %@",appInfo.appId,data);

TMAExternalJSPluginResult *pluginResult = [TMAExternalJSPluginResult new]; [TMAExternalJSPluginResult.result = [TMAExternalJSPluginResult new].
pluginResult.result = @{}; return
return pluginResult;
}

TMAExternalJSAPI_IMP(test) {
TMFMiniAppInfo *appInfo = context.tmfAppInfo; TMFMiniAppInfo *appInfo = context.
NSDictionary *data = params[@"data"];

NSLog(@"************ invokeNativePlugin test,appId:%@,data is %@",appInfo.appId,data);

// asynchronous processing, return the result to the mini program in an async callback async callbacks
//{
// TMAExternalJSPluginResult *pluginResult = [TMAExternalJSPluginResult new]; // pluginResult.result = [TMAExternalJSPluginResult new].
// pluginResult.result = @{@"result": result.data}; // [context doCallback
// [context doCallback:pluginResult].
// }
return nil;
}

@end
It can be used like this in a mini program.
// Asynchronous api calls
var opts = {
api_name: 'test',
success: function(res) {},
failure: function(res) {},
completion: function(res) {},
data: { // Input
Name : 'kka',
age : 22
}
}
wx.invokeNativePlugin(opts); // Synchronise api calls.

// Synchronise api calls
var opts = {
api_name: 'testSync',
sync:true
}
var rst = wx.invokeNativePlugin(opts); var rst = wx.

Advanced use

custom api supports configuration in the way of terminal app configuration file, which is invoked in the mini program by calling wx.api directly.
1. Unify the configuration file implemented in the app in customapi-config.json with the following reference:
{
"extApi":[{
"name": "test",
"sync": false,
"params": {
"data": ""
}
},
{
"name": "testSync",
"sync": true,
"params": {
"name": "",
"title": ""
}
}
]
}


2. Put customapi-config.json into the iOS project:

3. Set the configuration file path during SDK initialization.
[[TMFMiniAppSDKManager sharedInstance] setCustomApiConfigFile:[[NSBundle mainBundle] pathForResource:@"customapi-config" ofType:@"json"]];

4. Call it directly in the mini program with wx.test().
//Asynchronous API calls
var opts = {
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
data: {
name : 'kka',
age : 22
}
}
wx.test(opts);

//Synchronise api calls
var rst = testSync(opts);

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon