0
# iOS Platform Integration
1
2
iOS-specific APIs for integrating Compose Multiplatform UI with iOS applications, UIKit components, and native iOS functionality.
3
4
## Capabilities
5
6
### ComposeUIViewController
7
8
Primary entry point for embedding Compose content in iOS applications as a UIViewController.
9
10
```kotlin { .api }
11
/**
12
* Creates a UIViewController containing Compose content for integration with iOS apps
13
* @param configure Configuration block for iOS-specific settings
14
* @param content Composable content to display
15
* @return UIViewController that can be used in iOS navigation hierarchies
16
*/
17
fun ComposeUIViewController(
18
configure: ComposeUIViewControllerConfiguration.() -> Unit = {},
19
content: @Composable () -> Unit
20
): UIViewController
21
22
/**
23
* Configuration options for ComposeUIViewController
24
*/
25
class ComposeUIViewControllerConfiguration {
26
/**
27
* Controls validation of Info.plist CADisableMinimumFrameDurationOnPhone requirement
28
* Set to false to disable strict validation (not recommended for production)
29
*/
30
var enforceStrictPlistSanityCheck: Boolean
31
}
32
```
33
34
**Usage Examples:**
35
36
```kotlin
37
// Basic usage - create a view controller with Compose content
38
fun MainViewController(): UIViewController =
39
ComposeUIViewController {
40
MyComposeApp()
41
}
42
43
// Advanced configuration
44
fun ConfiguredViewController(): UIViewController =
45
ComposeUIViewController(
46
configure = {
47
enforceStrictPlistSanityCheck = false
48
}
49
) {
50
MyComposeApp()
51
}
52
```
53
54
### UIKit Interop Components
55
56
Components for embedding native UIKit views and view controllers within Compose content.
57
58
```kotlin { .api }
59
/**
60
* Embed a native UIView inside Compose content
61
* @param factory Factory function that creates the UIView instance
62
* @param modifier Modifier for styling and layout
63
* @param update Function called when the view needs to be updated
64
* @param onResize Callback for handling view resize events
65
* @param background Background color for the embedded view
66
*/
67
@Composable
68
fun UIKitView(
69
factory: () -> UIView,
70
modifier: Modifier = Modifier,
71
update: (UIView) -> Unit = {},
72
onResize: ((view: UIView, rect: CValue<CGRect>) -> Unit)? = null,
73
background: Color = Color.Unspecified
74
)
75
76
/**
77
* Embed a native UIViewController inside Compose content
78
* @param factory Factory function that creates the UIViewController instance
79
* @param modifier Modifier for styling and layout
80
* @param update Function called when the view controller needs to be updated
81
*/
82
@Composable
83
fun UIKitViewController(
84
factory: () -> UIViewController,
85
modifier: Modifier = Modifier,
86
update: (UIViewController) -> Unit = {}
87
)
88
```
89
90
**Usage Examples:**
91
92
```kotlin
93
// Embed a MapKit view
94
UIKitView(
95
factory = { MKMapView() },
96
modifier = Modifier
97
.padding(4.dp)
98
.border(2.dp, Color.Blue)
99
.size(300.dp),
100
update = { mapView ->
101
// Configure map properties
102
mapView.showsUserLocation = true
103
}
104
)
105
106
// Embed a UITextField with state synchronization
107
@OptIn(ExperimentalForeignApi::class)
108
@Composable
109
fun NativeTextField() {
110
var message by remember { mutableStateOf("Hello, World!") }
111
112
UIKitView(
113
factory = {
114
val textField = object : UITextField(CGRectMake(0.0, 0.0, 0.0, 0.0)) {
115
@ObjCAction
116
fun editingChanged() {
117
message = text ?: ""
118
}
119
}
120
textField.addTarget(
121
target = textField,
122
action = NSSelectorFromString(textField::editingChanged.name),
123
forControlEvents = UIControlEventEditingChanged
124
)
125
textField
126
},
127
modifier = Modifier.fillMaxWidth().height(30.dp),
128
update = { textField ->
129
textField.text = message
130
}
131
)
132
}
133
134
// Embed a complex camera view with resize handling
135
UIKitView(
136
modifier = Modifier.fillMaxSize(),
137
background = Color.Black,
138
factory = {
139
val cameraContainer = UIView()
140
cameraContainer.layer.addSublayer(cameraPreviewLayer)
141
cameraPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
142
captureSession.startRunning()
143
cameraContainer
144
},
145
onResize = { view: UIView, rect: CValue<CGRect> ->
146
CATransaction.begin()
147
CATransaction.setValue(true, kCATransactionDisableActions)
148
view.layer.setFrame(rect)
149
cameraPreviewLayer.setFrame(rect)
150
CATransaction.commit()
151
}
152
)
153
```
154
155
### iOS Window Management
156
157
iOS-specific window management and safe area handling.
158
159
```kotlin { .api }
160
/**
161
* System-defined window insets for iOS safe areas and system UI
162
*/
163
object WindowInsets {
164
val systemBars: WindowInsets
165
val statusBars: WindowInsets
166
val navigationBars: WindowInsets
167
val ime: WindowInsets
168
val safeDrawing: WindowInsets
169
val safeGestures: WindowInsets
170
val safeContent: WindowInsets
171
}
172
173
/**
174
* Apply window insets padding to respect iOS safe areas
175
*/
176
fun Modifier.windowInsetsPadding(insets: WindowInsets): Modifier
177
```
178
179
**Usage Examples:**
180
181
```kotlin
182
// Respect iOS safe areas
183
Column(
184
modifier = Modifier
185
.fillMaxSize()
186
.windowInsetsPadding(WindowInsets.systemBars),
187
horizontalAlignment = Alignment.CenterHorizontally
188
) {
189
// Content automatically respects safe areas
190
Text("This content respects iOS safe areas")
191
}
192
193
// Handle keyboard insets
194
LazyColumn(
195
modifier = Modifier
196
.fillMaxSize()
197
.windowInsetsPadding(WindowInsets.ime)
198
) {
199
// List content that adjusts for keyboard
200
}
201
```
202
203
### iOS-SwiftUI Integration
204
205
Pattern for integrating Compose UIViewController with SwiftUI applications.
206
207
**SwiftUI Integration Pattern:**
208
209
```swift
210
// SwiftUI wrapper for Compose content
211
struct ComposeView: UIViewControllerRepresentable {
212
func makeUIViewController(context: Context) -> UIViewController {
213
let controller = Main_iosKt.MainViewController()
214
controller.overrideUserInterfaceStyle = .light
215
return controller
216
}
217
218
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
219
// Handle updates if needed
220
}
221
}
222
223
// Use in SwiftUI
224
struct ContentView: View {
225
var body: some View {
226
ComposeView()
227
.ignoresSafeArea()
228
}
229
}
230
```
231
232
### Required iOS Imports
233
234
Essential imports for iOS platform integration:
235
236
```kotlin
237
// Core iOS integration
238
import androidx.compose.ui.window.ComposeUIViewController
239
import androidx.compose.ui.interop.UIKitView
240
import androidx.compose.ui.interop.UIKitViewController
241
242
// Native iOS APIs
243
import kotlinx.cinterop.ExperimentalForeignApi
244
import kotlinx.cinterop.ObjCAction
245
import kotlinx.cinterop.CValue
246
import platform.UIKit.*
247
import platform.Foundation.*
248
import platform.CoreGraphics.*
249
250
// For advanced integrations
251
import platform.AVFoundation.*
252
import platform.MapKit.*
253
```
254
255
### iOS Accessibility Support
256
257
Configuration for iOS accessibility integration:
258
259
```kotlin { .api }
260
/**
261
* Accessibility synchronization options for iOS
262
*/
263
enum class AccessibilitySyncOptions {
264
/**
265
* Always synchronize accessibility tree with iOS
266
*/
267
Always,
268
269
/**
270
* Synchronize only when accessibility services are enabled
271
*/
272
WhenAccessibilityServicesEnabled,
273
274
/**
275
* Never synchronize accessibility tree
276
*/
277
Never
278
}
279
280
/**
281
* Configure content with accessibility support
282
*/
283
@ExperimentalComposeApi
284
fun setContentWithAccessibility(
285
accessibilitySyncOptions: AccessibilitySyncOptions = AccessibilitySyncOptions.WhenAccessibilityServicesEnabled,
286
content: @Composable () -> Unit
287
)
288
```
289
290
### iOS Configuration Requirements
291
292
Important configuration requirements for iOS apps using Compose:
293
294
**Info.plist Requirement:**
295
```xml
296
<key>CADisableMinimumFrameDurationOnPhone</key>
297
<true/>
298
```
299
300
**Alternative Configuration:**
301
```kotlin
302
ComposeUIViewController(
303
configure = {
304
// Opt-out of strict plist validation (not recommended for production)
305
enforceStrictPlistSanityCheck = false
306
}
307
) {
308
content()
309
}
310
```
311
312
## Types
313
314
```kotlin { .api }
315
/**
316
* iOS-specific geometry types from CoreGraphics
317
*/
318
typealias CGRect = platform.CoreGraphics.CGRect
319
typealias CGPoint = platform.CoreGraphics.CGPoint
320
typealias CGSize = platform.CoreGraphics.CGSize
321
322
/**
323
* Common UIKit view types for interop
324
*/
325
typealias UIView = platform.UIKit.UIView
326
typealias UIViewController = platform.UIKit.UIViewController
327
typealias UITextField = platform.UIKit.UITextField
328
typealias UILabel = platform.UIKit.UILabel
329
```