Open Templates:

Before adding code, create a subdirectory within your app's root directory to place template files (.BWT).

Initialize a Template Object:

  • Create a dropdown menu on the UI to allow users to select a template.
Menu {
    Picker("Template Picker", selection: $selection) {
        //Adding the template name strings will go here. 
    }
} label: {Text(selection).font(.caption)}
  • Populate the dropdown menu with the available file names.
Menu {
    Picker("Template Picker", selection: $selection) {
        //*NEW*
        let templateList: [String] = [--INSERT NAMES OF AVAILABLE TEMPLATES--]
        ForEach(templateList, id: \.self) {
            Text($0).font(.footnote)
        }
    }
} label: {Text(selection).font(.caption)}
  • Open the resource as an InputStream for when the user selects a template from the dropdown menu,
public func getSelectedTemplate(context: AppContext, selection: String) -> Template? {
    do{
        //For this line to work, you MUST have a subdirectory at your projects root.
        //Otherwise, it will not be able to find the fileReferenceLiteralResourceName.
        let filePath = URL(fileReferenceLiteralResourceName: selection)
        let templateData: Data = try Data(contentsOf: filePath)
        let iStream: InputStream = InputStream(data: templateData)
    }
    catch let error {
        print(error)
    }
    return nil
}
  • Call the SDK's getTemplate() method to retrieve the Template object
public func getSelectedTemplate(context: AppContext, selection: String) -> Template? {
    do{
        //For this line to work, you MUST have a subdirectory at your projects root.
        //Otherwise, it will not be able to find the fileReferenceLiteralResourceName.
        let filePath = URL(fileReferenceLiteralResourceName: selection)
        let templateData: Data = try Data(contentsOf: filePath)
        let iStream: InputStream = InputStream(data: templateData)
        //*NEW*
        return TemplateFactory.getTemplate(template: iStream)
    }
    catch let error {
        print(error)
    }
    return nil
}
  • After successfully initializing the Template object, iterate through its placeholders to set any desired values. (Example: An EditText view to allow a user to set the value of a specific field in the template.)

Setting a Placeholder Value:

After a successful initialization of a template object, the getTemplateData() method can be used to set a specific value to a placeholder on the template. In this example, iterate through all template data until we find a placeholder with the name of "TEXT 1" and we change it to say "Example."

let placeholder = try template?.getTemplateData()[0]
if placeholder.getName() == "TEXT 1" {
    placeholder.setValue(value: "Example")
}

Preview the Template:

  • Call template.getPreview(labelNumber, dpi, maxPixelWidthAndHeight)

(Reference the Template page).

func templatePreview(selection: String) -> some View {
    do{
        //Where "template" is the Template object you found earlier.
        let bitmap: CGImage? = try template?.getPreview(labelNumber: 1, dpi: 96, maxPixelWidthAndHeight: 200)
        if bitmap == nil {
            return Image(uiImage: UIImage())
        }
        return Image(uiImage: UIImage(cgImage: bitmap!)).resizable()
    }
    catch let error {
        print(error)
    }
    return Image("ERROR")
}