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.7.1")
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
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.
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()
)
Logger
To provide a custom logger, use the following method:
NativeblocksManager.getInstance().provideEventLogger(
loggerType: "INTEGRATION_UNIQUE_KEY_TYPE",
logger: CustomLoggerInstance()
)
Change Language
To set the language used in your screens:
NativeblocksManager.getInstance().setLocalization(
languageCode: "EN"
)
Set global parameters for A/B testing
To set global parameters used to load a frame based on some conditions:
NativeblocksManager.getInstance().setGlobalParameters([
"language": "EN",
"country": "UAE",
"currency": "AED",
"appVersionCode": "45"
])
Experiment & Feature Flags
Get experiment or feature flag values with optional cache control:
// Use default cache (24 hours = 86400 seconds)
let showNewUI = await NativeblocksManager.getInstance().getExperiment(
key: "show_new_ui",
defaultValue: false
)
// No cache - always fetch fresh
let criticalFlag = await NativeblocksManager.getInstance().getExperiment(
key: "payment_enabled",
defaultValue: false,
cacheTTL: 0
)
// Custom cache duration (1 hour = 3600 seconds)
let theme = await NativeblocksManager.getInstance().getExperiment(
key: "theme_color",
defaultValue: "#FF0000",
cacheTTL: 3600
)
The getExperiment method supports:
- String: Maps to
STRINGorJSONvariable type - Int, Float, Double: Map to
NUMBERvariable type - Bool: Maps to
BOOLEANvariable type
Scaffold API
Retrieve the scaffold model containing frame definitions:
let (scaffold, errorMessage) = await NativeblocksManager.getInstance().getScaffold()
if let scaffold = scaffold {
// Access scaffold model with frames list
}
Frame Cache Management
Manage frame data synchronization and caching:
// Sync a specific frame from the server
await NativeblocksManager.getInstance().syncFrame(route: "/home")
// Clear a specific frame from cache
await NativeblocksManager.getInstance().clearFrame(route: "/home")
// Clear all frames from cache
await NativeblocksManager.getInstance().clearAllFrames()
Custom Type Converter
If your JSON contains a special type (for example, a size or color), you can register a converter:
NativeblocksManager.getInstance().provideTypeConverter(
Color.self,
converter: ColorNativeType()
)