All Products
Search
Document Center

Mobile Platform as a Service:The client calls the card method

Last Updated:Apr 24, 2023

This topic describes how to call the card method on the iOS client.

Procedure

  1. Implement the called JS method on the card side.

    <script>
        const navigator = requireModule("crystalnavigator");
        export default {
            data: {
                message: 'Hello Cube',
                text:"The Cube engine is an easy-to-use cross-platform development solution that can build high-performance with a Web-based development experience",
                string: "defaultString",
                temp:"https://gw.alicdn.com/tfs/TB1dZ4WowoQMeJjy0FnXXb8gFXa-950-1267.jpg"
            },       
            methods: {
                            // The JS method called by the client.
                jsTestMethod(string) {
                        // Update page parameters.
                  this.string = string;
               }
            }
        }
    </script>
  2. Pack the card and publish it to the card background.

  3. The client calls.

    Obtain the card instance in the agent method of card loading, call the corresponding JS method, and send the data.

    #pragma mark---CrystalCardCallback
    - (void)onLoaded:(CubeCard *)card cardType:(CCardType)cardType config:(CubeCardConfig *)config erroCode:(CubeCardResultCode)erroCode {
        if (!card) {
            NSString *errMsg = [NSString stringWithFormat:@"Failed to create: templteId=%@,style=%d, error=%d", [config templteId], cardType, erroCode];
            NSLog(@"Error message :%@", errMsg); 
            return;
        }
        
        NSLog(@"Succ %@ style %d error %d", [config templteId], cardType, erroCode);
    
        dispatch_async(dispatch_get_main_queue(), ^{        
            NSString *text = @"The client calls the card JS method to pass the parameter: Hello World";
            if (![text isEqualToString:@""]) {
                NSArray *valueArray = @[text];
                // Call the JS method agreed with the card and pass in the parameters.
                [card callJsFunction:@"jsTestMethod" arguments:valueArray];
            }
        });
    }