Skip to content

Commit

Permalink
Updated root integration quick start docs for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
arkivanov committed Apr 14, 2024
1 parent 35f1e11 commit 4fd26e6
Showing 1 changed file with 57 additions and 7 deletions.
64 changes: 57 additions & 7 deletions docs/getting-started/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,13 @@ fun main() {
}
```

### IOS with SwiftUI (with the experimental ApplicationLifecycle)
### iOS with Compose or SwiftUI (with the experimental ApplicationLifecycle)

!!!warning

Use this approach only if your root component lives in the app scope (e.g. have only one UIViewController holding the root component).

1. Declare a simple `AppDelegate` containing the `RootComponent`.
Step 1. In your Xcode project declare a simple `AppDelegate` containing the `RootComponent`.

```swift
class AppDelegate: NSObject, UIApplicationDelegate {
Expand All @@ -483,7 +483,32 @@ class AppDelegate: NSObject, UIApplicationDelegate {
}
```

2. Use `AppDelegate` in your `App` entrypoint.
Step 2. *If using Compose*, create a Kotlin file named `RootViewController.kt` in your shared module in `iosMain` source set with the following content. Then create `RootView` displaying `RootViewController`.

```kotlin
import androidx.compose.ui.window.ComposeUIViewController
import platform.UIKit.UIViewController

fun rootViewController(root: RootComponent): UIViewController =
ComposeUIViewController {
// Render the UI here
}
```

```swift
struct RootView: UIViewControllerRepresentable {
let root: RootComponent

func makeUIViewController(context: Context) -> UIViewController {
return RootViewControllerKt.rootViewController(root: root)
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
```

Step 3. Use `AppDelegate` in your application entrypoint.

```swift
@main
Expand All @@ -499,9 +524,9 @@ struct iOSApp: App {
}
```

### IOS with SwiftUI (without the experimental ApplicationLifecycle)
### iOS with Compose or SwiftUI (without the experimental ApplicationLifecycle)

1. Create `RootHolder` class that holds the root component and its lifecycle.
Step 1. Create `RootHolder` class that holds the root component and its lifecycle.

```swift
class RootHolder : ObservableObject {
Expand All @@ -525,15 +550,40 @@ class RootHolder : ObservableObject {
}
```

2. Declare a simple `AppDelegate` containing `RootHolder`
Step 2. Declare a simple `AppDelegate` containing `RootHolder`

```swift
class AppDelegate: NSObject, UIApplicationDelegate {
let rootHolder: RootHolder = RootHolder()
}
```

3. Use `AppDelegate` in your `App` entrypoint.
Step 3. *If using Compose*, create a Kotlin file named `RootViewController.kt` in your shared module in `iosMain` source set with the following content. Then create `RootView` displaying `RootViewController`.

```kotlin
import androidx.compose.ui.window.ComposeUIViewController
import platform.UIKit.UIViewController

fun rootViewController(root: RootComponent): UIViewController =
ComposeUIViewController {
// Render the UI here
}
```

```swift
struct RootView: UIViewControllerRepresentable {
let root: RootComponent

func makeUIViewController(context: Context) -> UIViewController {
return RootViewControllerKt.rootViewController(root: root)
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
```

Step 4. Use `AppDelegate` in your `App` entrypoint.

```swift
@main
Expand Down

0 comments on commit 4fd26e6

Please sign in to comment.