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 "ExamplePlaceholder" and we change it to say "Example."
for placeholder in try template?.getTemplateData() {
if placeholder.getName() == "ExamplePlaceholder" {
placeholder.setValue(value: "Example")
}
}
If you would like to display to your user what type of object the current placeholder is, use the following code to retrieve a TemplateObjectType. This enumeration represents the type of object/placeholder. This is useful if a template placeholder was renamed in BWS that doesn't represent the type of object very well. For example a barcode in BWS could be renamed to "ExamplePlaceholder" in BWS. Your user would have no idea that this object is a barcode without TemplateObjectData.getTemplateObjectType().
for placeholder in try template?.getTemplateData() {
if placeholder.getName() == "ExamplePlaceholder" {
//This can return the values StaticText, Text, Rectangle, Barcode, Image, PolyPolyLine, or Other
let placeholderType = placeholder.getTemplateObjectType()
}
}
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")
}