SwiftUI Rendering
Starting with 3.7.0, the SDK can render every consent screen with SwiftUI. The primary surface is a set of binding-driven view modifiers — pure SwiftUI, no view controllers — and a parallel set of show…View methods for presenting the SwiftUI screens from UIKit code. Both render the same screens with the same behavior and callbacks as the UIKit API.
You choose the rendering per call. The UIKit show…ViewController methods are unchanged and fully supported: the SDK never switches rendering underneath an existing integration.
Requirements
- iOS 15 or later. The SwiftUI surface is annotated
@available(iOS 15, *). On iOS 13–14, use the UIKit methods — gate your call sites withif #available(iOS 15, *)if your deployment target is below 15. - Swift only. The SwiftUI surface is not exposed to Objective-C. Objective-C integrations continue to use the UIKit
show…ViewControllermethods, which are unaffected by this feature.
Pure SwiftUI: the view modifiers
Attach a modifier to any view and drive it with a Bool binding, the same way you would use .sheet(isPresented:). The binding flips back to false when the user closes the screen (save, close, deny, or an interactive swipe), and setting it to false yourself dismisses the screen.
struct ConsentSettingsView: View {
let consentUiBuilder: ConsentUiBuilder
@State private var showDialog = false
@State private var showPreferences = false
var body: some View {
List {
Button("Show Consent Dialog") { showDialog = true }
Button("Manage Cookie Preferences") { showPreferences = true }
}
.osanoConsentDialog(
isPresented: $showDialog,
consentUiBuilder: consentUiBuilder,
type: .modal // .modal, .fullScreen, or .bottomSheet
) { categories in
print("Consented: \(categories)") // [String] of category names
} deny: {
print("User denied")
}
.osanoDrawer(isPresented: $showPreferences, consentUiBuilder: consentUiBuilder) { categories in
print("Saved: \(categories)")
}
}
}
The IAB TCF screens follow the same pattern, with an optional jumpTo identifier that opens the screen scrolled to a purpose or vendor:
.osanoIABDisclosures(isPresented: $showDisclosures, consentUiBuilder: consentUiBuilder, jumpTo: "2")
.osanoIABThirdParties(isPresented: $showThirdParties, consentUiBuilder: consentUiBuilder)
| Modifier | Screen |
|---|---|
osanoConsentDialog(isPresented:consentUiBuilder:type:success:deny:) | Consent dialog |
osanoDrawer(isPresented:consentUiBuilder:success:) | Storage preferences panel |
osanoIABDisclosures(isPresented:consentUiBuilder:jumpTo:) | IAB TCF disclosures |
osanoIABThirdParties(isPresented:consentUiBuilder:jumpTo:) | IAB TCF vendors |
osanoConsentDialog waits for the configuration and translations to finish loading before presenting, and uses the same presentation styles as the UIKit dialog. The screens present with the SDK's own transition and background treatments, which is why these are dedicated modifiers rather than content for .sheet or .fullScreenCover.
The ConsentAppSwift example app ships a complete version of this pattern behind its SwiftUI Example button (SwiftUIExampleView.swift).
Showing consent only when needed
If you want the dialog to appear automatically — but only for users who actually need to (re)consent — use the gated variant. It applies the same validity check as ConsentManager.showConsentIfNeeded: the dialog shows when the user has never consented, when their consent predates a server-forced re-consent, or when it has expired. Users with valid consent see nothing.
In pure SwiftUI, attach osanoConsentDialogIfNeeded to a view — typically your root view — and it evaluates the gate once when that view appears. There is no binding to manage:
ContentView()
.osanoConsentDialogIfNeeded(
consentManager: consentManager,
consentUiBuilder: consentUiBuilder,
type: .modal
) { categories in
print("Consented: \(categories)")
} deny: {
print("User denied")
}
From UIKit code, showConsentIfNeededView is the SwiftUI-rendering parallel of showConsentIfNeeded — same gate, same deferred evaluation while the consent-expiration check resolves, returning true when the prompt will show:
if #available(iOS 15, *) {
consentManager.showConsentIfNeededView(
presenterViewController: self,
consentUiBuilder: consentUiBuilder,
type: .modal
) { categories in
print("Consented: \(categories)")
} deny: {
print("User denied")
}
}
Both are opt-in and never fire on their own; if your consent UI is driven elsewhere (for example the web layer), simply don't call them.
Alongside UIKit: the show…View methods
In a UIKit-based app (or a mixed codebase), you can render the SwiftUI screens without any SwiftUI plumbing: each modifier has a show…View counterpart on ConsentUiBuilder that takes the presenting view controller, mirroring the UIKit show…ViewController methods you already call.
Each method presents a screen immediately, so call them from separate user actions — presenting while another screen is already up gets refused by UIKit.
// From a "Show consent dialog" action:
if #available(iOS 15, *) {
consentUiBuilder.showDialogView(
presenterViewController: self,
type: .modal
) { categories in
print("Consented: \(categories)")
} deny: {
print("User denied")
}
}
// From a "Manage preferences" action:
if #available(iOS 15, *) {
consentUiBuilder.showDrawerView(presenterViewController: self) { categories in
print("Saved: \(categories)")
}
}
// From IAB TCF actions — one screen per action:
if #available(iOS 15, *) {
consentUiBuilder.showIABDisclosuresView(presenterViewController: self, jumpTo: "2")
}
if #available(iOS 15, *) {
consentUiBuilder.showIABThirdPartiesView(presenterViewController: self)
}
Unlike the UIKit drawer flow — where you present categoriesViewController yourself after calling showDrawerViewController(success:) — showDrawerView presents the panel for you.
Choosing between the surfaces
| UIKit | SwiftUI from UIKit | Pure SwiftUI | |
|---|---|---|---|
| API | show…ViewController methods | show…View methods | osano… view modifiers |
| Minimum OS | iOS 13 | iOS 15 | iOS 15 |
| Objective-C | Yes | No | No |
| Callbacks | NSArray | [String] | [String] |
| Support | Fully supported | Fully supported | Fully supported |
All three record consent identically and render the same screens — pick whichever fits each call site, including mixing them within one app.