Alipay+ Super App PlatformAlipay+ Super App Platform

renderWidget

Use the renderWidget API to create and render view compontents based on templates.

Method signature

The following code shows the method signature of the renderWidget API:

copy
func renderWidget(containerView: UIView, dataModel: WidgetDataModel, actionDelegate: WidgetActionDelegate, completion: ((Result<Void, WidgetError>) -> Void)? = nil) 

Request parameters

Name

Type

Description

containerView

UIView

A pre-allocated UIView placed in a designated area on the page to reserve space. Once the widget is created, the ASAPWidget component will render it onto the containerView as soon as possible.

dataModel

WidgetDataModel

Data model of the widget

actionDelegate

WidgetActionDelegate

SPIfor handling user click events

completion

(Result<Void, WidgetError>) -> Void

A closure that takes a Result containing either a Void object on success or a WidgetError on failure.

The Result type helps to concisely represent success or failure without the need for separate error handling.

Response parameters

N/A

Exception

Error Code

Error message

1400

A widget render error has occurred.

Sample

Swift

copy
// Create a UIView in advance and place it in a specified location on the page to reserve a space. Once the widget is created, the ASAPWidget  component will render it onto the containerView as soon as possible 
let containerView = UIView()
view.addSubview(containerView)
var dataModel = WidgetDataModel(templateCode: "ac-sign", templateVersion: "1.0.0")
dataModel.content = ["templateCode":"ac-sign_in","templateVersion":"1.0.0","content":["viewType":"viewTypeDummy"]]
dataModel.extendedInfo = ["extras1":"extras1Dummy"]
dataModel.spmInfo = ["spm1":"spm1Dummy"]
ComponentManager.ASAP.widgetComponent.renderWidget(containerView: containerView, dataModel: dataModel, actionDelegate: self, completion: { result in
    switch result {
    case .success:
        print("success")
    case .failure(let error):
        print(error.debugDescription)
    }
})