tencent cloud

Feedback

Customize API

Last updated: 2024-03-08 14:20:07
    The TCMPP SDK engine offers an extension mechanism, permitting the host app to customize APIs for Mini Program invocation.

    Implementation Steps

    1. Customize class
    2. In the class implementation, introduce the header file #import "TMAExternalJSPlugin.h".
    3. Register API.

    Referential example

    #import "NativePluginTest.h"
    #import "TMAExternalJSPlugin.h"
    #import "TMFMiniAppInfo.h"
    
    @implementation NativePluginTest
    
    TMA_REGISTER_EXTENAL_JSPLUGIN;
    
    //Customize API configuration
    TMAExternalJSAPI_IMP(testSync) {
    TMFMiniAppInfo *appInfo = context.tmfAppInfo;
    NSDictionary *data = params[@"data"];
    
    NSLog(@"************ invokeNativePlugin testSync,appId:%@,data is %@",appInfo.appId, data);
    
    TMAExternalJSPluginResult *pluginResult = [TMAExternalJSPluginResult new];
    pluginResult.result = @{};
    return pluginResult;
    }
    
    TMAExternalJSAPI_IMP(test) {
    TMFMiniAppInfo *appInfo = context.tmfAppInfo;
    NSDictionary *data = params[@"data"];
    
    NSLog(@"************ invokeNativePlugin test,appId:%@,data is %@",appInfo.appId, data);
    
    // Handle asynchronously and return the results to the Mini Program in the asynchronous callback.
    //{
    //  TMAExternalJSPluginResult *pluginResult = [TMAExternalJSPluginResult new];
        //  pluginResult.result = @{@"result" : result.data};
        // [context doCallback:pluginResult];
    // }
    return nil;
    }
    
    @end
    Used within the Mini Program.
    //Asynchronous API invocation
    var opts = {
    api_name: 'test',
    success: function(res) {},
    fail: function(res) {},
    complete: function(res) {},
    data: { // Input Parameters
    name : 'kka',
    age : 22
    }
    }
    wx.invokeNativePlugin(opts);
    
    //Call API synchronously
    var opts = {
    api_name: 'testSync',
    sync:true
    }
    var rst = wx.invokeNativePlugin(opts);

    Advanced Usage

    Custom APIs can be configured via the terminal app's configuration file and called directly in the Mini Program through the wx.api method.
    1. Consolidate the configuration files implemented in the app into tcmpp-custom-config.json, and package the tcmpp-custom-config.json file into the app. Refer to the following:
    {
        "extApi":[{
                "name": "test",
                "sync": false,
                "params": {
                    "data": ""
                }
            },
            {
                "name": "testSync",
                "sync": true,
                "params": {
                    "name": "",
                    "title": ""
                }
            }
        ]
    }
    
    
    2. Called within the Mini Program.
    //Asynchronous API invocation
    var opts = {
    success: function(res) {},
    fail: function(res) {},
    complete: function(res) {},
    data: {
    name : 'kka',
    age : 22
    }
    }
    wx.test(opts); 
    
    //Call API synchronously
    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