### Import SplineView
Source: https://docs.spline.design/exporting-your-scene/android/native-3d-embeds-for-android
Include this import statement at the top of any file where the SplineView component is utilized.
```kotlin
import design.spline.runtime.SplineView
```
--------------------------------
### Initialize SplineController for SwiftUI
Source: https://docs.spline.design/exporting-your-scene/apple-platform/code-api-for-swift-ui
Connects a SplineController to a SplineView to enable programmatic interaction. A single controller instance must be used for only one view.
```swift
struct ContentView: View {
private var controller = SplineController()
var body: some View {
let url = URL(string: "https://build.spline.design/4-2CGowqS9z7MGA62D-B/scene.splineswift")!
SplineView(sceneFileURL: url, controller: controller)
}
}
```
--------------------------------
### Load Spline Scene from Cloud
Source: https://docs.spline.design/exporting-your-scene/android/native-3d-embeds-for-android
Use this snippet to load a Spline scene directly from a URL. Ensure the `loadUrl` function is uncommented and the `loadResource` function is commented out.
```kotlin
import design.spline.runtime.SplineView
@Composable
fun MyView() {
AndroidView(
factory = { ctx ->
SplineView(context).apply {
// fetching from cloud
loadUrl("https://build.spline.design/nDkzGcy787CUJYaEI-gy/scene.splinecontent")
// fetching from local
// loadResource(R.raw.scene)
}
}
)
}
```
--------------------------------
### Load Spline Scene Locally
Source: https://docs.spline.design/exporting-your-scene/android/native-3d-embeds-for-android
This snippet loads a Spline scene from a local .splinecontent file. Ensure the `loadResource` function is uncommented and the `loadUrl` function is commented out. The `R.raw.[file-name]` should correspond to your .splinecontent file name.
```kotlin
import design.spline.runtime.SplineView
@Composable
fun MyView() {
AndroidView(
factory = { ctx ->
SplineView(context).apply {
// fetching from cloud
// loadUrl("https://build.spline.design/nDkzGcy787CUJYaEI-gy/scene.splinecontent")
// fetching from local
loadResource(R.raw.scene)
}
}
)
}
```
--------------------------------
### Configure Android Manifest Permissions
Source: https://docs.spline.design/exporting-your-scene/android/native-3d-embeds-for-android
Add internet and network state permissions to your AndroidManifest.xml to enable cloud-based scene loading.
```xml
```
--------------------------------
### Load Spline Scene with Callback
Source: https://docs.spline.design/exporting-your-scene/android/code-api-for-kotlin
Loads a Spline scene and executes a callback once the scene is fully loaded, enabling immediate interaction with the API. Use this to ensure the scene is ready before attempting to access its elements.
```kotlin
import android.util.Log
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import com.spline.view.SplineView
@Composable
fun SplineScene() {
AndroidView(
factory = { ctx ->
val view = SplineView(context = ctx)
view.loadResource(R.raw.scene) {
// Scene is loaded, you can now use the API
val obj = view.findObjectByName("Cube")
Log.d("Spline", "Found object: ${obj?.name}")
}
view
},
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
)
}
```
--------------------------------
### Embed a Spline scene in SwiftUI
Source: https://docs.spline.design/exporting-your-scene/apple-platform/code-api-for-swift-ui
Initializes a basic SplineView using a scene file URL.
```swift
struct ContentView: View {
var body: some View {
let url = URL(string: "https://build.spline.design/4-2CGowqS9z7MGA62D-B/scene.splineswift")!
SplineView(sceneFileURL: url)
}
}
```
--------------------------------
### SplineView Scene Loading
Source: https://docs.spline.design/exporting-your-scene/android/code-api-for-kotlin
Methods for loading Spline scenes from different sources.
```APIDOC
## SplineView Scene Loading
### Description
Methods for loading Spline scenes from local resources or URLs.
### Methods
#### loadResource
##### Description
Loads a Spline scene from a raw resource file. A callback is invoked upon completion.
##### Parameters
- **id** (string) - Required - The identifier for the resource file.
- **onComplete** (function) - Required - The callback function to execute when loading is finished.
#### loadUrl
##### Description
Loads a Spline scene from a specified URL. A callback is invoked upon completion.
##### Parameters
- **url** (string) - Required - The URL of the Spline scene file.
- **onComplete** (function) - Required - The callback function to execute when loading is finished.
```
--------------------------------
### Embed Spline Scene from Cloud
Source: https://docs.spline.design/exporting-your-scene/apple-platform/native-3d-embeds-for-i-os
Use this snippet to load a scene directly from a URL. Ensure the SplineRuntime library is imported into your project.
```swift
import SplineRuntime
import SwiftUI
struct ContentView: View {
var body: some View {
// // fetching from local
// let url = Bundle.main.url(forResource: "scene", withExtension: "splineswift")!
// fetching from cloud
let url = URL(string: "https://build.spline.design/nDkzGcy787CUJYaEI-gy/scene.splineswift")!
SplineView(sceneFileURL: url).ignoresSafeArea(.all)
}
}
```
--------------------------------
### Import AndroidView for Compose
Source: https://docs.spline.design/exporting-your-scene/android/native-3d-embeds-for-android
Include the AndroidView import when embedding Spline scenes within a Jetpack Compose project.
```kotlin
import androidx.compose.ui.viewinterop.AndroidView
```
--------------------------------
### Import Spline iOS Runtime
Source: https://docs.spline.design/exporting-your-scene/apple-platform/native-3d-embeds-for-i-os
Include this import statement at the top of any file where the Spline Runtime functionality is utilized.
```swift
import SplineRuntime
```
--------------------------------
### Manage Multiple Spline Views in Jetpack Compose
Source: https://docs.spline.design/exporting-your-scene/android/code-api-for-kotlin
Handle multiple independent `SplineView` instances within a Jetpack Compose UI. Each view requires its own reference for separate control.
```kotlin
@Composable
fun MultipleSplineScenes() {
var cubesView: SplineView? = null
var spheresView: SplineView? = null
Column {
AndroidView(
factory = { ctx ->
val view = SplineView(context = ctx)
cubesView = view
view.loadResource(R.raw.cubes)
view
},
modifier = Modifier.weight(1f).fillMaxWidth()
)
AndroidView(
factory = { ctx ->
val view = SplineView(context = ctx)
spheresView = view
view.loadResource(R.raw.spheres)
view
},
modifier = Modifier.weight(1f).fillMaxWidth()
)
Button(onClick = {
cubesView?.emitEvent(SplineEventName.keyUp, "Cube1")
}) {
Text("Go Cube")
}
Button(onClick = {
spheresView?.emitEvent(SplineEventName.keyUp, "Sphere1")
}) {
Text("Go Sphere")
}
}
}
```