All Products
Search
Document Center

Custom bi-directional channel for iOS Mini Program

Last Updated: Feb 05, 2021

If the existing Mini Program API or events cannot meet your development requirements, you can also extend it yourself.

Mini Program calls native custom API

  1. The client customizes the API and registers.
    Refer to Custom JSAPI to register your custom API.
  2. Mini Program call.
       
    1. my.call('tinyToNative', {
    2. param1: 'p1aaa',
    3. param2: 'p2bbb'
    4. }, (result) => {
    5. console.log(result);
    6. my.showToast({
    7. type: 'none',
    8. content: result.message,
    9. duration: 3000,
    10. });
    11. })

Native projects send custom events to Mini Program

  1. Mini Program registers the event.

       
    1. my.on('nativeToTiny', (res) => {
    2. my.showToast({
    3. type: 'none',
    4. content: JSON.stringify(res),
    5. duration: 3000,
    6. success: () => {
    7. },
    8. fail: () => {
    9. },
    10. complete: () => {
    11. }
    12. });
    13. })
  2. The client sends the event.
    Get the viewController where the current Mini Program page is located, and call the callHandler method to send the event.

       
    1. [self callHandler:@"nativeToTiny" data:@{@"key":@"value"} responseCallback:^(id responseData) {
    2. }];

    Parameter description::

    Parameter Description
    handlerName The name of the event monitored by the Mini Program.
    data The parameters passed by the client to the Mini Program.
    callback The call back processing block after the Mini Program executes the event.

Unregister custom event

If you no longer need custom events, please refer to Unregister custom events.