⌘K

Swift action compiler

Nativeblocks provides compiler to connect any codes with its core SDK

The Nativeblocks compiler is a tool that generates server-driven blocks and actions based on Swift code. It produces JSON and Swift files for each block and action, preparing them for upload to Nativeblocks servers.

How it works

If your project uses its own 'Package.swift' file, you can add NativeblocksCompiler as a dependency there. Add NativeblocksCompiler to your dependencies list

dependencies: [
    .package(
        url: "https://github.com/nativeblocks/nativeblocks-compiler-ios.git",
        .upToNextMajor(from: "1.1.0")
    ),
],

Any targets in your application that will use NativeblocksCompiler need to have a dependency on the Nativeblocks product.

.target(
    name: "MyApp",
    dependencies: [
        .product(name: "NativeblocksCompiler", package: "nativeblocks-compiler-ios")
    ]
)

Usage

actions are functions that can be invoked within a Nativeblocks Studio to perform specific tasks or operations. These annotations provide metadata and define configurable properties, data inputs, and events for your actions, making them usable as building actions in a visual editor.

@NativeAction

Purpose: Marks a class or function as an action.

Parameters:

  • name: The display name of the action in the visual editor.
  • keyType: A unique key used to identify the action type.
  • description: A brief description of the action's functionality.

Example:

@NativeAction(
    keyType : "NATIVE_ALERT",
    name : "Alert",
    description: "Nativeblocks alert action"
)

@NativeActionProp

Purpose: Defines a configurable property for the action. These properties can be set visually in the editor.

Parameters:

  • description: (Optional) A description of the property.
  • valuePicker: (Optional) Specifies the type of UI element used to edit the property (e.g., dropdown, text field).
  • valuePickerGroup: (Optional) Specifies the group name of the property to group all related properties.
  • valuePickerOptions: (Optional) Provides options for dropdown value pickers.

Example:

@NativeActionProp(description : "api key") apiKey: String

@NativeActionData

Purpose: Marks a parameter as a data input for the action. This data can be provided directly without visual configuration, often from other actions or data sources.

Parameters:

  • description: (Optional) A description of the data input.

Example:

@NativeActionData(description : "user input value") userPrompt: String

@NativeActionEvent

Purpose: Defines an event that the action can trigger, such as success, failure, or next updates.

Parameters:

  • description: (Optional) A description of the data input.
  • then: Specifies when the event should be triggered (e.g., success, failure, next, end).
  • dataBinding: (Optional) An array of data input names to bind to the event handler parameters.

Example:

@NativeActionEvent(
    then : Then.SUCCESS,
    dataBinding = ["result"]
)

This example demonstrates an alert action with configurable properties (animated), data inputs (message), and events for next (completion).

import Nativeblocks
import NativeblocksCompiler
import SwiftUI
import UIKit

@NativeAction(
    name: "Alert",
    keyType: "NATIVE_ALERT",
    description: "Nativeblocks alert action"
)
public class NativeAlert {
    var alertController: UIAlertController
    init(alertController: UIAlertController) {
        self.alertController = alertController
    }

    @NativeActionParameter
    struct Parameter {
        @NativeActionData
        var message: String
        @NativeActionProp
        var animated: Bool = false
        @NativeActionEvent
        var completion: (() -> Void)? = nil
    }

    @NativeActionFunction
    func show(
        param: Parameter
    ) {
        alertController.message = param.message
        alertController.present(
            animated: param.animated,
            completion: { param.completion?() }
        )
    }
}

Generated files

After providing annotations for blocks and actions, you can use the 'GenerateProvider' plugin to generate Swift code. These can then be initialized in App or via dependency injection

Note: The prefix for the provider name comes from the target name that was selected. In this case, since we provided "MyApp," the compiler generates with the "MyApp" prefix.

MyAppBlockProvider.provideActions(alert)

For actions, all dependencies must be provided during initialization. To optimize performance, consider using dependency injection for scoped or lazy instances.