tencent cloud

Feedback

Custom Mini Program APIs

Last updated: 2024-11-21 17:58:44
    The TCMPP 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 tcmpp-custom-config.json with the following reference:
    {
    "extApi":[{
    "name": "test",
    "sync": false,
    "params": {
    "data": ""
    }
    },
    {
    "name": "testSync",
    "sync": true,
    "params": {
    "name": "",
    "title": ""
    }
    }
    ]
    }
    
    
    2. Put tcmpp-custom-config.json into the iOS project:
    
    
    
    3. 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);
    
    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 avaliable.

    7x24 Phone Support