⌘K

Swift + SwiftUI SDK

Nativeblocks supports Swift and SwiftUI, for SDUI and SDA

Get started

Install or update Xcode to its latest version.

Make sure that your project meets these requirements:

  • Uses Swift 5.0 or higher
  • Uses SwiftUI for UI development
  • Use iOS 15.0 or above

Adding Nativeblocks to Your Project

Add the following dependency to your Package.swift or Xcode project:

.package(url: "https://github.com/nativeblocks/nativeblocks-ios-sdk", from: "1.2.0")

Initialize the SDK

Let's get started by initializing the SDK. You can do this in the App struct or provide via dependency injection.

NativeblocksManager.initialize(
    edition: .cloud(
        endpoint: NATIVEBLOCKS_API_URL,
        apiKey: NATIVEBLOCKS_API_KEY,
        developmentMode: true
    )
)

And call destroy function on applicationWillTerminate

App.swift
class AppDelegate: NSObject, UIApplicationDelegate {
    func applicationWillTerminate(_ application: UIApplication) {
        NativeblocksManager.getInstance().destroy()
    }
}

To initialize the SDK, three parameters are needed:

  • API Url (you can get specific version from support team)
  • API Key (Once you created a new project in the Nativeblocks, you can get the API key from project information)
  • DevelopmentMode (by enabling this option you can see the changes immediately, but for the production usage, this should be false)

Once the SDK is initialized, configure the main content of your app to use NativeblocksFrame to load and manage blocks.

App.swift
struct ContentView: View {
    var body: some View {
        NativeblocksFrame(
            route: "/",
            loading: {
                AnyView(NativeblocksLoading())
            },
            error: { message in
                AnyView(NativeblocksError(message: message))
            }
        )
    }
}

The core SDK itself does not have any integrations. All integrations need to be registered in the Nativeblocks integration marketplace and provided to the SDK.

The current version of the SDK supports the following integrations:

  • Block
  • Action & ActionContractor
  • Logger

Block

To provide a custom block, use the following method:

NativeblocksManager.getInstance().provideBlock(
    blockKeyType: "INTEGRATION_UNIQUE_KEY_TYPE",
    block: CustomBlockInstance()
)

Action

To provide a custom action, use the following method:

NativeblocksManager.getInstance().provideAction(
    actionKeyType: "INTEGRATION_UNIQUE_KEY_TYPE",
    action: CustomActionInstance()
)
NativeblocksManager.getInstance().provideActionContractor(
    actionContractor: CustomActionContractorInstance()
)

Logger

To provide a custom logger, use the following method:

NativeblocksManager.getInstance().provideEventLogger(
    loggerType: "INTEGRATION_UNIQUE_KEY_TYPE",
    logger: CustomLoggerInstance()
)